CSS实现垂直居中的常用方法总结(看了面试必过的那种)

写在前面

     很多前端面试中,都会问到css设置垂直居中的方法,这里做一个总结,你这个小机灵鬼一定要学会啊。

正文

     首先,如果 .parent 的 height 不写,你只需要 padding: 10px 0; 就能将 .child 垂直居中;如果 .parent 的 height 写死了,就很难把 .child 居中,以下是垂直居中的方法。
     注意:能不写 height 就千万别写 height。
     接下来,将具体讲述几种方法:

  1. 使用table自带的功能,或者将div封装成table
 html代码
<table class="parent">
    <tr>
      <td class="child">
      一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
      </td>
    </tr>
  </table>
  
css
.parent{
	  border: 1px solid red;
	  height: 600px;
}
.child{
  	border: 1px solid green;
}

将div封装成table
html代码
<div class="table">
      <div class="td">
        <div class="child">
          一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
        </div>
    </div>
  </div>

css
div.table{
  display: table;
  border: 1px solid red;
  height: 600px;
}

div.tr{
  display: table-row;
  border: 1px solid green;
}

div.td{
  display: table-cell;
  border: 1px solid blue;
  vertical-align: middle;
}
.child{
  border: 10px solid black;
}
  1. margin-top,margin-left设置-50%
 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>

css
.parent{
	  height: 600px;
	  border: 1px solid red;
	  position: relative;
}
.child{
	  border: 1px solid green;
	  width: 300px;
	  position: absolute;
	  background:red;
	  top: 50%;
	  left: 50%;
	  height: 100px;
	  margin-left: -150px;
	  margin-top: -50px;
}
  1. translate:-50%
 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>

css
.parent{
	  height: 600px;
	  border: 1px solid red;
	  position: relative;
}
.child{
	  border: 1px solid green;
	  position: absolute;
	  top: 50%;
	  left: 50%;
	  transform: translate(-50%,-50%);
}
  1. absolute margin auto
 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>

css
.parent{
	  height: 600px;
	  border: 1px solid red;
	  position: relative;
}
.child{
	  border: 1px solid green;
	  position: absolute;
	  width: 300px;
	  height: 200px;
	  margin: auto;
	  top: 0;
	  bottom: 0;
	  left: 0;
	  right: 0;
}
  1. :after   :before加上inline-block
 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>
  
css
.parent{
	  border: 3px solid red;
	  height: 600px;
	  text-align: center;
}

.child{
	  border: 3px solid black;
	  display: inline-block;
	  width: 300px;
	  vertical-align: middle;
}

.parent:before{
	  content:'';
	  outline: 3px solid red;
	  display: inline-block;
	  height: 100%;
	  vertical-align: middle;
}
.parent:after{
	  content:'';
	  outline: 3px solid red;
	  display: inline-block;
	  height: 100%;
	  vertical-align: middle;
}
  1. 使用flex
 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>
  
css
.parent{
	  height: 600px;
	  border: 3px solid red;
	  display: flex;
	  justify-content: center;
	  align-items: center;
}
.child{
	  border: 3px solid green;
	  width: 300px;
}

7.grid也是可以的(不过存在兼容问题,不推荐)

 html代码
 <div class="parent">
    <div class="child">
      这里被我垂直居中了。
    </div>
  </div>
  
css
.parent{
	  height: 600px;
	  border: 3px solid red;
	  display: grid;
}
.child{
	  border: 3px solid green;
	  width:300px;
	  align-self: center;
      justify-self: center;
}
  1. 使用writing-mode
html代码
<div class="g-father">
    <div class="parent">
        <div class="child">这里垂直居中了</div>
    </div>
</div>

css
.g-father {
    writing-mode: vertical-lr;
    text-align: center;
}
.parent{
    writing-mode: horizontal-tb;
    display: inline-block;
    text-align: center;
    width: 100%;
}
.child{
    display: inline-block;
    margin: auto;
    text-align: left;
}

写在最后

     到这里,就已经讲了8种方法了,只要记住四五种,多练练,面试你没问题的。附加一张效果图:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值