CSS居中常用方法

只有普通流和绝对定位的元素可以设置居中,浮动元素是不存在居中的。

1.块级元素居中
1) 普通流元素(position : static 或 position : relative)
  • 水平居中:{ margin : 0 auto; }
2) 绝对定位元素(position : absolute 或 position : fixed)
  • 水平、垂直居中:该元素{ margin: auto; top: 0; bottom: 0; left: 0; right: 0; overflow : auto },其父元素{ position : relative;}
2.内联级元素居中
  • 水平居中:在其父元素上设置{ text-align : center; }
  • 行内垂直居中:{height : 30px; line-height : 30px; }

猜你喜欢

转载自blog.csdn.net/ruanhao1203/article/details/79483501