前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何实现水平垂直居中如何实现一个块级元素的水平垂直居中

如何实现水平垂直居中如何实现一个块级元素的水平垂直居中

原创
作者头像
前端小tips
发布2021-11-27 18:42:46
1.1K0
发布2021-11-27 18:42:46
举报
文章被收录于专栏:前端文章小tips前端文章小tips

1、利用 display:table-cell;属性来实现

display:table-cell;结合vertical-align: middle;使用实现垂直居中,margin:0 atuo;可以实现子元素的水平居中。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>table-cell居中</title>
        <style type="text/css">
            .content{
                border: 1px solid blue;
                display: table;
                margin: 50px auto;
            }
            .table{
                height: 300px;
                width: 300px;
                display: table-cell;
                vertical-align: middle;
                border: 1px solid red;
            }
            .box{
                height: 150px;
                width: 150px;
                background: #109D71;
                margin: 0 auto;
                display: table;
            }
            .word{
                display: table-cell;
                vertical-align: middle;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div class="content">
            <div class="table">
                <div class="box">
                    <div class="word">
                        垂直水平居中
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

image.png

2、flex 弹性布局

详情查看https://www.runoob.com/w3cnote/flex- grammar.html

代码语言:javascript
复制
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>flex布局</title>
        <style>
            .content {
                width: 300px;
                height: 300px;
                margin: 50px;
                border: 1px solid #109D71;
                display: flex;
                align-items: center;
                justify-content: center;
                
            }
            .box{
                width: 150px;
                height: 150px;
                background-color: #109D71;
                text-align: center;
                margin: 0 auto;
                display: flex;
                align-items: center;
                justify-content: center;
            }
        </style>
    </head>
    <body>
        <div class="content">
            <div class="box">
                水平垂直居中
            </div>
        </div>
    </body>
</html>

image.png

3、利用绝对定位,让元素脱离普通文档流,再结合margin:auto。

代码语言:javascript
复制
<!DOCTYPE html>
<head>
    <title>absolute居中</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        .content {
            width: 300px;
            height: 300px;
            border: 1px solid  #109D71;
            position:relative;
        }
​
        .box {
            margin:auto;
            width: 100px;
            height: 100px;
            background: #109D71;
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            top: 0;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="box">
        </div>
    </div>
</body>
</html>

4、absolute+margin 通过计算元素宽高实现居中

让子元素居中时,margin必须要知道子元素的宽高,切忌不能用百分比。

代码语言:javascript
复制
<!DOCTYPE html>
<head>
    <title>absolute+margin计算元素宽高判断居中</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        .content {
            width: 300px;
            height: 300px;
            border: 1px solid #109D71;
            position: relative;
        }
​
        .box {
            position: absolute;
            width: 100px;
            height: 100px;
            /* top: 50%;
            left: 50%;
            margin-top: -50px;
            margin-left: -50px; */
            top: calc(50% - 50px);
            left: calc(50% - 50px);
            background: #109D71;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="box">
        </div>
    </div>
</body>
</html>

5、absolute + translate ,通过translate将元素移动自身的50%,50%,实现水平垂直居中。

translate(-50%,-50%) 属性:向上(x轴)和左(y轴),移动自身长宽的 50%,使其居于中心位置。 top: 50%;left: 50%;:是以窗口左上角为原点,需要减掉自身宽高的一半,才能居中。 与使用margin实现居中不同的是, margin必须知道自身的宽高,而translate可以在不知道宽高的情况下进行居中,tranlate函数中的百分比是相对于自身宽高的百分比

代码语言:javascript
复制
<!DOCTYPE html>
<head>
    <title>absolute+translate</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
        .content {
            width: 300px;
            height: 300px;
            border: 1px solid #109D71;
            position: relative;
        }
​
        .box {
            position: absolute;
            width: 100px;
            height: 100px;
            top: 50%;
            left: 50%;
            /* 渐进增强 */
            -webkit-transform: translate(-50%, -50%);
            -ms-transform: translate(-50%, -50%);
            transform: translate(-50%, -50%);
            background: #109D71;
        }
    </style>
</head>
<body>
    <div class="content">
        <div class="box">
        </div>
    </div>
</body>
</html>

个人推荐用2、3方法, flex布局法适合在局部使用。 绝对定位适合在全屏场景使用,比如弹框中。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • 2、flex 弹性布局
  • 3、利用绝对定位,让元素脱离普通文档流,再结合margin:auto。
  • 4、absolute+margin 通过计算元素宽高实现居中
  • 5、absolute + translate ,通过translate将元素移动自身的50%,50%,实现水平垂直居中。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档