盒子几种居中的方法

本文介绍了CSS中实现元素水平居中和水平垂直居中的五种常见方法,包括margin auto、text-align center、绝对定位配合margin、transform translate以及使用flexbox布局。这些方法覆盖了不同场景下的居中需求,帮助开发者更好地理解和应用CSS布局技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

水平居中

方法一:给div设置宽度后将margin的上下设为0,左右设为auto

 <div class="box"></div>
 body{
         margin: 0;
         padding: 0;
        }
.box{
      width: 300px;
      height: 200px;
      background-color: aqua;
      margin: 0 auto;
    }

效果:
在这里插入图片描述
方法二:使小盒子变为行内块从而在大盒子中使用text-aglin属性

 <div class="container">
        <div class="box"></div>
    </div>
 body{
            margin: 0;
            padding: 0;
        }
        .container{
            text-align: center;
            background: rgba(0,0,0,0.2);
        }
        .box{
            width: 300px;
            height: 200px;
            background-color: aqua;
             display: inline-block;
           }

在这里插入图片描述

水平垂直居中

方法一:

<div class="box"></div>
 body{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 300px;
            height: 200px;
            margin:auto;
            position: absolute;
            top:0;
            bottom: 0;
            left: 0;
            right: 0;
            background-color: salmon;
           }

在这里插入图片描述
方法二:

<div class="box"></div>
 body{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 300px;
            height: 200px;
            margin:-100px -150px;/*为宽高的一半*/
            position: absolute;
            top:50%;
            left:50%;
            background-color:palevioletred;
           }

在这里插入图片描述
方法三:

 body{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 300px;
            height: 200px;
            position: absolute;
            top:50%;
            left:50%;
            transform: translate(-50%,-50%);
            background-color:plum;
           }

在这里插入图片描述
方法四:

body{
            margin: 0;
            padding: 0;
        }
        .container{
            display: flex;
            align-items:center;  /*垂直居中*/
            justify-content: center; /*水平居中*/
            height: 600px;
           background-color: chocolate;
        }
        .box{
            width: 300px;
            height: 200px;
            background-color:seagreen;
           }

在这里插入图片描述

方法五:

<div class="container">
        <div class="box"></div>
    </div>
 body{
            margin: 0;
            padding: 0;
        }
        .container{
           position: fixed;
           top: 0;
           right: 0;
           left: 0;
           bottom: 0;
           background-color: slategray;
           text-align: center;
           overflow: auto;
           white-space:nowrap;
           font-size: 0;
        }
        .container::after{
            content: "";
            display: inline-block;
            height: 100%;
            vertical-align: middle;
        }
        .box{
            width: 300px;
            height: 200px;
            display: inline-block;
            vertical-align: middle;
            background-color:yellow;
            white-space: normal;
           }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值