我有一個場景,其中有一個940像素的寬度框,最多有4個子像素爲140像素,其間有20像素,浮動在一條線上。如果有1,2,3或4個子div,我希望CSS強制將該子div組居中。目前,當第4個被移除時,前3個的位置不會改變。當孩子的數量可能不同時,將div的孩子置於中心位置
我正在使用LESS。
我的代碼:
#parent
{
position: relative;
width: 940px;
clear: both;
background-image: url(../images/bg.png);
background-repeat: repeat-x;
height: 100px;
margin: 0 auto;
margin-top: 2px;
}
#parent h1
{
position: absolute;
color: white;
font-size: 36px;
font-weight: 400;
margin: 0;
padding: 0;
left: 5px;
top: -24px;
}
#children
{
position: relative;
margin: 0 auto;
margin-top: 35px;
}
.child
{
width: 140px;
padding-right:20px;
float: left;
text-align: center;
}
#children:last-child
{
padding-right: 0px;
clear: right;
}
編輯:和HTML ...
<div id="parent">
<h1>Lorem Ipsum</h1>
<div id="children">
<div class="child" id="1"></div>
<div class="child" id="2"></div>
<div class="child" id="3"></div>
<div class="child" id="4"></div>
</div>
</div>
此外,小童人數:最後一個孩子似乎無所作爲。我是否應該將其規則應用於#4?
我們應該猜到你的HTML,或者是你能告訴我們你正在使用的是什麼? –
你是如何添加div的。你是動態添加這些還是事先定義這些?..? –
添加了HTML。 – Richard