css:盒子垂直居中的几种方法

本文详细介绍了网页布局中将元素居中的五种常见方法:1) 定宽高+绝对定位+margin负值;2) 绝对定位+transform translate;3) 绝对定位+margin auto;4) 使用Flexbox布局;5) 利用Grid布局。通过实例代码展示了每种方法的实现细节,帮助开发者灵活选择适合场景的居中方案。
摘要由CSDN通过智能技术生成
   <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 方法一: 定宽高- 绝对定位+-margin */
        .father {
            width: 200px;
            height: 200px;
            background-color: lightcoral;
            position: relative;
        
        }
        .son {
            width: 100px;
            height: 100px;
            background-color: lightgreen;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -50px;
            margin-top: -50px;
        } 
       
    </style>
</head>

<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>

</html>

方法二:子元素绝对定位后, 使用transform 中的translate都为-50%后实现。

    /* 2. 绝对定位+transform */
        
        .father {
            width: 200px;
            height: 200px;
            background-color: lightcoral;
            position: relative;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: lightgreen;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

方法三: 子元素绝对定位后, top, left , right 和bottom都设置为0, margin 为auto

/* 3. 绝对定位+ margin:auto */
        
        .father {
            width: 200px;
            height: 200px;
            background-color: lightcoral;
            position: relative;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: lightgreen;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }

方法四:flex布局: 父元素加入flex布局后, 使用水平居中和垂直居中。

 .father {
            width: 200px;
            height: 200px;
            background-color: lightcoral;
            display: flex;
            justify-content: center;   //水平居中
            align-items: center;     //垂直居中
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: lightgreen;
        }

 方法五:栅格布局 grid

         父元素加入栅格, 子元素margin auto

.father {
            width: 200px;
            height: 200px;
            background-color: lightcoral;
            display: grid;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: lightgreen;
            margin: auto;
        }

 方法六:让元素以表格单元形式出现, 在父元素中,文本水平居中,垂直居中; 子元素margin:auto

 /* 6. table-cell */
        
        .father {
            width: 200px;
            height: 200px;
            background-color: lightcoral;
            display: table-cell;
            /* 文本水平居中 */
            text-align: center;  
            /* 垂直居中 */
            vertical-align: middle;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: lightgreen;
            margin: auto;
        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值