我有一个包含一个div元素的包装器,该div元素又包含了一些div。这些div在运行时会被添加或删除。HTML和CSS如下所示:
我正在使用
如何使
感谢您的建议。
<div id="Wrapper">
<div class="InnerGreen">
<div class="InnerContent"></div>
<div class="InnerContent"></div>
</div>
</div>
#Wrapper{
width:600px;
height:50px;
margin:10px 20px;
background:blue;}
.InnerGreen{
background:green;
margin:10px auto; // this doesn't center
overflow:hidden;
display:inline-block;}
.InnerContent{
background:yellow;
height:30px;
width:40px;
float:left;
margin:3px 5px;}
我正在使用
inline-block
将.InnerGreen
包裹在Wrapper
中,但是margin:auto
似乎无法水平居中div。当然,如果我定义了.InnerGreen
的宽度,这个方法是行得通的,但实际上.InnerContent
divs是各种不同尺寸的div的集合,所以我无法在运行时设置.InnerGreen
的宽度。如何使
margin:auto
起作用?这里是jsfiddle。感谢您的建议。