css中居中方法小结

---恢复内容开始---

1.文字垂直居中

.header_nav-item{
         height:38px;
         line-height:38px;
  }

 即文字所在模块的高度和行高设置成一样的!

2.块元素垂直居中与父元素 

.header_wrap{
      height:80px;
      position:realitive;
}
 
.header_logo{
      height:38px;
      position:absolute;
      top:50%;
      margin-top:-19px
}

  即父元素设置成相对定位,子元素设置成绝对定位,且子元素中top为50%,margin-top为-(负)height的一半。是一种比较经典的块元素居中法!

3.块元素(容器)水平居中    

.header_warp{
   margin: 0 auto;
}

即元素上下为0,左右设置成自动!

总之是很基础的方法,但经常用到,应该熟记!

---恢复内容结束---

原文地址:https://www.cnblogs.com/jianghao233/p/7731999.html