2013-02-28 85 views
0

我試着這樣做兩個div自適應寬度

<div> 
    <div style="width:50%;"> first div </div> 
    <div style="width:50%;"> second div </div> 
</div> 

有時動態第一或第二個div將不顯示。 當第一個div沒有顯示時,我需要第二個假設寬度爲100%,反之亦然。 我可以用css來做這個嗎? min-weigth或最大寬度或類似的東西?

+0

您可以隨時使用javascript。 – Vucko 2013-02-28 19:56:31

回答

0

您可以使用:only-child僞類

.childDiv 
{ 
    width:50%; 
} 
.childDiv:only-child 
{ 
    width:100%; 
} 

HTML

<div> 
    <div class="childDiv'> first div </div> 
    <div class="childDiv'> second div </div> 
</div> 
0

嘗試使用自動保證金CSS屬性:

.myClass 
{ 
    margin:0px auto; 
    width:50 //You can set this to whatever or take it out 
} 

,並加入到HTML

<div> 
    <div class="myClass'> first div </div> 
    <div class="myClass'> second div </div> 
</div>