CSS实现水平垂直同时居中的5种思路_html/css_WEB-ITnose
× 目录 [1]水平对齐+行高 [2]水平+垂直对齐 [3]margin+垂直对齐 [4]absolute [5]flex
前面的话
水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的5种思路
思路一: text-align + line-height实现单行文本水平垂直居中
<style>.test{ text-align: center; line-height: 100px;}</style>
<div class="test" style="background-color: lightblue;width: 200px;">测试文字</div>
思路二: text-align + vertical-align
【1】在父元素设置text-align和vertical-align,并将父元素设置为table-cell元素,子元素设置为inline-block元素
[注意]若兼容IE7-浏览器,将结构改为