<div>
<div style="float: left; width: 200px; background: red;">
left
</div>
<div style="background:green; overflow:hidden;">
i am right; i am right; i am right;
<br />
i am right; i am right; i am right;
<br />
i am right; i am right; i am right;
<br />
</div>
</div>
<div>
<div style="float: left; width: 200px; background: red;">
left
</div>
<div style="background:green; margin-left: 200px;">
i am right; i am right; i am right;
<br />
i am right; i am right; i am right;
<br />
i am right; i am right; i am right;
<br />
</div>
</div>
原理同上,通过 margin-left 使得 右 元素打破环绕。
1.3 absolute + margin-left (左边固定,右边自适应)
<div>
<div style="position: absolute; width: 200px; background: red;">
left
</div>
<div style="background:green; margin-left: 200px;">
i am right; i am right; i am right;
<br />
i am right; i am right; i am right;
<br />
i am right; i am right; i am right;
<br />
</div>
</div>
<div style="position: relative;">
<div style="position: absolute; left: 0; width: 200px;background: red;">
left
</div>
<div style="position: absolute; left: 200px; right: 0;background: blue;">
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
</div>
</div>
通过给两个子元素设置为 absolute, 绝对定位来自适应宽度。
1.5 float + 负外边距 (左或右自适应宽度均可)
1.5.1 左边自适应,右边定宽
.clearfix {
content: "";
display: block;
clear: both;
}
<div class="clearfix">
<div style="float: left; width: 100%;">
<div style="margin-right: 200px;background: red;">
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
</div>
</div>
<div style="float: left;background: blue; width: 200px; margin-left: -200px;">
I’m the Sidebar!
</div>
</div>
<div class="clearfix">
<div style="float: left;background: blue; width: 200px; margin-right: -100%;">
I’m the Sidebar!
</div>
<div style="float: left; width: 100%;">
<div style="margin-left: 200px;background: red;">
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
</div>
</div>
</div>
<div style="display: flex;">
<div style="width: 100%;background: red;">
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
</div>
<div style="background: blue; flex: 0 0 200px;">
I’m the Sidebar!
</div>
</div>
缺点:
1. 需要注意IE兼容性
通过设置 flex: 0 0 200px, 即 不伸长,不收缩,宽度为 200px
1.7 table (左边固定宽度,右边自适应)
<table>
<tbody>
<tr>
<td width="200px" style="background: red;">i am the sidebar</td>
<td style="background: blue;">
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
</td>
</tr>
</tbody>
</table>
<div>
<div style="display: table-cell;width: 200px;background: red;">
left
</div>
<div style="display: table-cell;background: blue;">
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
Main content in here;
</div>
</div>