时间:2021-07-01 10:21:17 帮助过:41人阅读
- <meta charset=" utf-8"><meta name="author" content="http://www.softwhy.com/"><title>蚂蚁部落</title> <style type="text/css"> .father{ width:600px; height:600px; background-color:green; margin:0px auto; position:relative; } .children{ position:absolute; width:200px; height:200px; left:200px; top:200px; background-color:red; } </style> <div class="father"> <div class="children"></div> </div>
以上代码可以将子div在父div中水平垂直居中对齐,下面就简单介绍一下几个要点:
1.父元素是使用相对定位,这样子元素就可以以它作为位移参考对象。具体可以参阅CSS的绝对定位 。
2.top属性值计算方式:父元素的高度/2-子元素高度/2,left属性值计算方式:父元素宽度值/2-子元素宽度值/2。
原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=6952