标签:左右 ace ems style 就是 width lex 参考 https
技术参考:https://www.php.cn/css-tutorial-409962.html
1.兼容性不错的主流css绝对定位居中的用法:
1 .conter{ 2 width: 600px; height: 400px; 3 position: absolute; left: 50%; top: 50%; 4 margin-top: -200px; /* 高度的一半 */ 5 margin-left: -300px; /* 宽度的一半 */ 6 }
注意:这种方法有一个很明显的不足,就是需要提前知道元素的尺寸。否则margin负值的调整无法精确。此时,往往要借助JS获得。
2.css3的出现,使得有了更好的解决方法,就是使用transform代替margin. transform中translate偏移的百分比值是相对于自身大小的,可以这样实现css绝对定位居中:
1 .conter{ 2 width: 600px; height: 400px; 3 position: absolute; left: 50%; top: 50%; 4 transform: translate(-50%, -50%); /* 50%为自身尺寸的一半 */ 5 }
3.margin:auto实现绝对定位元素的居中(上下左右均0位置定位;margin: auto):
1 .conter{ 2 width: 600px; height: 400px; 3 position: absolute; left: 0; top: 0; right: 0; bottom: 0; 4 margin: auto; /* 有了这个就自动居中了 */ 5 }
4.使用css3盒模型:flex布局实现css绝对定位居中。这种情况是在不考虑低版本浏览器的情况下可以使用:
1 .conter{ 2 display:flex; 3 justify-content: space-between; 4 align-items: center; 5 } 6 7 <div class="conter"> 8 <div></div> 9 </div>
元素.conter的子元素会居中定位
标签:左右 ace ems style 就是 width lex 参考 https
原文地址:https://www.cnblogs.com/qxp140605/p/11793774.html