CSS-实例-div 水平居中 垂直靠上

1 需求


2 语法


3 示例

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表格水平居中、垂直靠上示例</title>
    <style>
        /* 设置包含表格的div样式,使其内容水平居中 */
        .container {
            text-align: center; /* 用于行内或内联元素,但不影响块级元素如表格 */
            display: flex;
            justify-content: center; /* 使用flex布局使块级元素居中 */
            align-items: flex-start; /* 垂直靠上 */
            height: 100vh; /* 设置容器高度为视口高度,以便看到垂直靠上的效果 */
            margin: 0; /* 移除默认边距 */
        }

        /* 表格样式 */
        table {
            border-collapse: collapse; /* 合并边框 */
            margin: 0 auto; /* 上下边距为0,左右自动,使表格在水平方向上居中 */
        }

        th, td {
            border: 1px solid black; /* 添加边框以便查看 */
            padding: 8px; /* 添加内边距 */
        }
    </style>
</head>
<body>

<div class="container">
    <table>
        <caption>标题</caption>
        <thead>
        <tr>
            <th>表头1</th>
            <th>表头2</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>数据1</td>
            <td>数据2</td>
        </tr>
        <!-- 其他行和列... -->
        </tbody>
    </table>
</div>

</body>
</html>

4 参考资料

相关推荐
混迹在开发队伍里的伪开发12 小时前
css的var用法,定义属性,全局使用
前端·css
混迹在开发队伍里的伪开发19 小时前
css 设置两端对齐
前端·css
迟_20 小时前
CSS-实现图片靠右
前端·css
鱼毓屿御21 小时前
Tailwind CSS配置进阶
前端·css
_OP_CHEN1 天前
【前端开发之CSS】(五)CSS 盒模型深度解析:从基础到实战,掌控页面布局核心
前端·css·html·盒模型·页面开发·页面布局·页面美化
她超甜i2 天前
css省略号展示,兼容性强,js判断几行,不需要定位
javascript·css·vue.js
Mr Xu_2 天前
深入分析Element UI Tree组件在本地与生产环境样式差异问题
css·ui·elementui
梦6502 天前
CSS 元素垂直水平居中的 8 种方法
前端·css
前端 贾公子2 天前
深入浅出 CSS 属性:pointer-events: none
前端·css
LBJ辉2 天前
CSS - code
前端·css