Div占据整个剩余宽度

sub*_*ime 9 css

我有一个父div和2个div.第一个儿童div是50px宽和100%高度.第二个孩子div是100%身高,我要采取宽度的其余部分(100% - 50px)我该怎么做?

这是我创建的小提琴:http://jsfiddle.net/muGty/ 基本上我希望蓝色div(右)完全占据灰色容器的其余部分.

<div class="parent">
    <div class="left"></div>
    <div class="right"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

Mr.*_*ien 20

你的意思是这样的吗?

<div id="left">
</div>
<div id="right">
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

html, body {
    height: 100%;
}

#left {
    width:200px;
    float:left;
    background: #f00;
    height: 100%;
}
#right {
    margin-left: 200px;
    background: #0f0;
    height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

更新:

你也可以calc()在CSS3中使用属性,这将简化这个过程

html, body {
    height: 100%;
}

#left {
    width:200px;
    float:left;
    background: #f00;
    height: 100%;
}

#right {
    float: left;
    background: #0f0;
    height: 100%;
    width: calc(100% - 200px); /* Negate the fixed width element value from 100% */
}
Run Code Online (Sandbox Code Playgroud)

演示2

  • 只是一个注释......具有"float"属性的元素必须位于标记的顶部.正是Alien先生所写的方式. (7认同)