页面元素水平垂直居中

在页面布局中,元素水平垂直居中的方法有很多,这里就列举3个简单,用的比较多的方法吧。

情况一、已知元素的宽高

复制代码
复制代码
 1     <style>
 2         .center {
 3             width: 200px;
 4             height: 200px;
 5             Courier New", serif; font-size: inherit; line-height: 1.5;">;
 6             /* 1.绝对定位,加margin */
 7             position: absolute;
 8             left: 50%;
 9             top: 50%;
10             margin-top: -100px;
11             margin-left: -100px;
12 } 13 </style> 14 </head> 15 <body> 16 <div class="center"></div> 17 </body>
复制代码
复制代码

情况二、未知元素的宽高

复制代码
复制代码
body {
            /* 3.flex */
            width: 600px;
            height: 600px;
            display: flex;
             /* 主轴居中 */
            justify-content: center;
            /* 侧轴剧中 */
            align-items: center       

        }
        .center {
            width: 200px;
            height: 200px;
            background-color: #000;
            /* 2.绝对定位+transform: ; */
            /* position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%,-50%); */
             }
复制代码
复制代码
posted @   W(王甜甜)  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示