CSS实现水平垂直居中的方式

一、元素定宽高

1.absolute + 负margin

PC端有兼容性要求,宽高固定,推荐

<style type="text/css">
.box{
    border: 1px solid red;
    width: 300px;
    height: 300px;
    position: relative;   //关键代码
}
.box1.size{
    width: 100px;
    height: 100px;
    background: red;
}
.box1 {
    position: absolute;   //关键代码
    top: 50%;             //关键代码
    left: 50%;            //关键代码
    margin-left: -50px;   //关键代码
    margin-top: -50px;    //关键代码
}

</style>

<body>
  
  <div class="box">
    <div class="box1 size">absolute + 负margin</div>
  </div>

</body>

2.absolute + margin:auto

<style type="text/css">
.box{
    border: 1px solid red;
    width: 300px;
    height: 300px;
    position: relative;  //关键代码
}
.box1.size{
    width: 200px;
    height: 200px;
    background: red;
}
.box1 {
    position: absolute;  //关键代码
    top:0;               //关键代码
    left: 0;             //关键代码
    right: 0;            //关键代码
    bottom: 0;           //关键代码
    margin:auto;         //关键代码
}

</style>

<body>
  
  <div class="box">
    <div class="box1 size">absolute + margin:auto</div>
  </div>

</body>

3.absolute + calc

<style type="text/css">
.box{
    border: 1px solid red;
    width: 300px;
    height: 300px;
    position: relative;           //关键代码
}
.box1.size{
    width: 100px;
    height: 100px;
    background: red;
}
.box1 {
    position: absolute;          //关键代码
    top: calc(50% - 50px);       //关键代码
    left: calc(50% - 50px);      //关键代码
}

</style>

<body>
  
  <div class="box">
    <div class="box1 size">absolute + calc</div>
  </div>

</body>

二、元素不定宽高

1.absolute + transform

<style type="text/css">
.box{
    border: 1px solid red;
    width: 300px;
    height: 300px;
    position: relative;                     //关键代码
}
.box1 {
    position: absolute;                    //关键代码
    top: 50%;                             //关键代码
    left: 50%;                           //关键代码
    transform: translate(-50%, -50%);    //关键代码
}

</style>

<body>
  
  <div class="box">
    <div class="box1">absolute + transform</div>
  </div>

</body>

2.lineheight + vertical-align

适用在子元素中将文字显示居中

<style type="text/css">
.box{
    border: 1px solid red;
    width: 300px;
    height: 300px;
    line-height: 300px;                   //关键代码
    text-align: center;                   //关键代码
    font-size: 0px;                       //关键代码
}
.box1 {
    font-size: 16px;                     //关键代码
    display: inline-block;               //关键代码
    vertical-align: middle;              //关键代码
    line-height: initial;                //关键代码
}

</style>

<body>
  
  <div class="box">
    <div class="box1">absolute + transform</div>
  </div>

</body>

3.flex布局

PC端无兼容性要求,移动端推荐使用

<style type="text/css">
.box{
    border: 1px solid red;
    width: 300px;
    height: 300px;
    display: flex;                   //关键代码         
    justify-content: center;         //关键代码
    align-items: center;             //关键代码
}
</style>

<body>
  
  <div class="box">
    <div class="box1">flex布局</div>
  </div>

</body>

4.css-table

PC端有兼容要求,宽高不固定

<style type="text/css">
.box{
    border: 1px solid red;
    width: 300px;
    height: 300px;
    display: table-cell;         //关键代码
    text-align: center;          //关键代码
    vertical-align: middle;      //关键代码
}
.box1{ 
    display: inline-block;
}
</style>

<body>
  
  <div class="box">
    <div class="box1">css-table</div>
  </div>

</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值