css实现垂直水平居中的六种方法

第一种 定位方法

<!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>
        .box1 {
            background-color: #bfa;
            width: 500px;
            height: 500px;
            position: relative;
        }
​
        .box2 {
            background-color: red;
            width: 100px;
            height: 100px;
            position: absolute;
            top: 50%;
            left: 50%;
            //修改外边距。往回平移半个子盒子的高度和宽度
            margin-left: -50px;
            margin-top: -50px;
        }
    </style>
</head>
​
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
​
</html>

第二种 margin:auto方法

<!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>
        .box1{
            background-color: #bfa;
            width: 500px;
            height: 500px;
            position: relative;
        }
        .box2{
            background-color: red;
            width: 100px;
            height: 100px;
            position:absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

第三种 display:table-cell方法

<!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>
        .box1{
            background-color: #bfa;
            width: 500px;
            height: 500px;
            display: table-cell;
            //把此元素放置在父元素水平的中部。
            vertical-align: middle;
            //垂直居中
            text-align: center;
           
        }
        .box2{
            background-color: red;
            width: 100px;
            height: 100px;
            display: inline-block;
        }
    </style>
</head>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

vertical-align 属性设置一个元素的垂直对齐方式。

该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐。允许指定负长度值和百分比值。这会使元素降低而不是升高。在表单元格中,这个属性会设置单元格框中的单元格内容的对齐方式。

第四种 flex方法

<!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>
        .box1 {
            background-color: #bfa;
            width: 500px;
            height: 500px;
            display: flex;
            //主轴居中
            justify-content: center;
            //辅轴居中
            align-items: center;
            
        }
​
        .box2 {
            background-color: red;
            width: 100px;
            height: 100px;
          
        }
    </style>
</head>
​
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
​
</html>

第五种 手动计算

<!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>
        .box1 {
            background-color: #bfa;
            width: 500px;
            height: 500px;
            
        }
​
        .box2 {
            background-color: red;
            width: 100px;
            height: 100px;
            margin-left: 200px;
            margin-top: 200px;
            
        }
        .box1::before{
            content: "";
            display:table;
        }
    </style>
</head>
​
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
​
</html>

*第六种 translate方法

<!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>
        .box1 {
            background-color: #bfa;
            width: 500px;
            height: 500px;
           position: relative;
        }
​
        .box2 {
            background-color: red;
            width: 100px;
            height: 100px;
          position: absolute;
          top: 50%;
          left: 50%;
          //往回平移半个子盒子的高度和宽度
          transform: translate(-50%,-50%);
        }
    </style>
</head>
​
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
​
</html>

这是我总结出来的六种方法,大家还知道什么方法呢,评论区留言吧!

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值