4

我想要两个并排的 div 并为一个 div 定义宽度。

然后,另一个 div 将自动适应父容器。我怎么做?

HTML:

<div class="parent">
    <div class="first"></div>
    <div class="last"></div>
</div>

CSS:

.parent {
    width: 100%;
}
.parent .first {
    width: 300px;
    float: left;
    background-color: red;
}
.parent .last {
    width: auto;
    float: right;
    background-color: blue;
}

谢谢。

4

2 回答 2

6

不要float第二个div。只有第一个需要浮动。

http://jsfiddle.net/zLef8/

于 2013-02-21T05:04:31.210 回答
2

试试这个

<div class="parent">
    <div class="first">test</div>
    <div class="last">test2</div>
</div>

CSS

.parent {
    width: 100%;
}
.parent > .first {
    width: 300px;
    float: left;
    background-color: red;
}
.parent > .last {
    width: auto;
    float: right;
    background-color: blue;
    text-align:left
}

示例jsfiddle

于 2013-02-21T05:15:06.770 回答