两个div自适应宽度

时间:2013-02-28 19:54:32

标签: css

我正在尝试这样做

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

有时动态的第一个或第二个div不会显示。 当第一个div没有显示时,我需要第二个假设宽度为100%,反之亦然。 我能用css做到这一点吗? min-weigth或max-width或类似的东西?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>