元素居中方式有哪些?
元素居中的方法有很多,块级元素和行内元素居中的方法是不同的!
水平居中
div{ margin: 0 auto }
水平垂直居中
方式一:
div{ /* 定位 */ position: absolute; margin: auto; left: 0; right: 0; top:0; bottom:0; }
方式二:
div{ /* 定位 */ position:absolute; top:50%; left:50%; transform:translate(-50%,-50%); }
垂直居中IMG
img{ /* display:table-cell 或者 position定位 */ display: table-cell; text-align: center; vertical-align: middle; }
使用flex布局居中元素
div{ display:flex; jusitify-content:center; align-item:center; align-content:center; }
本文来自博客园,作者:不知名前端李小白,转载请注明原文链接:https://www.cnblogs.com/libo-web/p/15610854.html