我想使用DIV制作一个双列布局,其中右侧列将具有固定的200像素宽度,左侧列将占用其余所有空间。
如果使用表格,这很容易实现:
<table width="100%">
<tr>
<td>Column 1</td>
<td width="200">Column 2 (always 200px)</td>
</tr>
</table>
但是DIV呢?能否实现这一点?如果可以,那么如何实现呢?
我想使用DIV制作一个双列布局,其中右侧列将具有固定的200像素宽度,左侧列将占用其余所有空间。
如果使用表格,这很容易实现:
<table width="100%">
<tr>
<td>Column 1</td>
<td width="200">Column 2 (always 200px)</td>
</tr>
</table>
但是DIV呢?能否实现这一点?如果可以,那么如何实现呢?
#wrapper {
margin-right: 200px;
}
#content {
float: left;
width: 100%;
background-color: #CCF;
}
#sidebar {
float: right;
width: 200px;
margin-right: -200px;
background-color: #FFA;
}
#cleared {
clear: both;
}
<div id="wrapper">
<div id="content">Column 1 (fluid)</div>
<div id="sidebar">Column 2 (fixed)</div>
<div id="cleared"></div>
</div>
固定在左侧
#wrapper {
margin-left: 200px;
}
#content {
float: right;
width: 100%;
background-color: #CCF;
}
#sidebar {
float: left;
width: 200px;
margin-left: -200px;
background-color: #FFA;
}
#cleared {
clear: both;
}
<div id="wrapper">
<div id="content">Column 1 (fluid)</div>
<div id="sidebar">Column 2 (fixed)</div>
<div id="cleared"></div>
</div>
另一种解决方案是使用display: table-cell,这会产生等高列。
下面是一个解决方案(但它有一些怪癖,如果你注意到了并且它们是个问题,请让我知道):
<div>
<div style="width:200px;float:left;display:inline-block;">
Hello world
</div>
<div style="margin-left:200px;">
Hello world
</div>
</div>
inline-block
在 IE6 和 7 中可以工作,只是需要一些帮助,但在这里不必要,因为浮动元素已经胜出,并且当使用浮动时,显示属性会被忽略。我会再检查一下。 - clairesuzydisplay: inline-block
,但如果遇到双倍边距 bug,display:inline
对于 IE6 及以下版本可能会有帮助 - 如果 遇到该问题。 - clairesuzyCSS:
#sidebar {float: right; width: 200px; background: #eee;}
#content {overflow: hidden; background: #dad;}
HTML:
<div id="sidebar">I'm 200px wide</div>
<div id="content"> I take up the remaining space <br> and I don't wrap under the right column</div>
以上代码应该可行。如果您想让它有宽度并居中,可以将该代码放在包装器中。overflow:hidden
是设置没有宽度的列垂直包含并防止其围绕侧栏(可以是左侧或右侧)换行的关键。
如果需要IE6支持,zoom:1
可能也需要设置在#content div上。
CSS解决方案
#left{
float:right;
width:200px;
height:500px;
background:red;
}
#right{
margin-right: 200px;
height:500px;
background:blue;
}
请查看这个网站的实际例子:http://jsfiddle.net/NP4vb/3/
jQuery解决方案
var parentw = $('#parent').width();
var rightw = $('#right').width();
$('#left').width(parentw - rightw);
请查看这个工作示例 http://jsfiddle.net/NP4vb/
。 <div style="width: 100%">
<div style="width: 50%; float: left; display: inline-block;">
Hello world
</div>
<div style="width: 50%; display: inline-block;">
Hello world
</div>
</div>
最近我看到了一个使用CSS的流式布局网站。 http://matthewjamestaylor.com/blog/perfect-multi-column-liquid-layouts(请查看下面链接中的演示页面)。
作者现在提供了一个固定宽度布局的示例。看看; http://matthewjamestaylor.com/blog/how-to-convert-a-liquid-layout-to-fixed-width。
这提供了以下示例, http://matthewjamestaylor.com/blog/ultimate-2-column-left-menu-pixels.htm(我认为适合您所需的两列布局)
http://matthewjamestaylor.com/blog/fixed-width-or-liquid-layout.htm(适用于三列布局)。
很抱歉给大家提供了这么多链接到这个人的网站,但我认为它是一个非常棒的资源。
#wrapper {
margin-right: 50%;
}
#content {
float: left;
width: 50%;
background-color: #CCF;
}
#sidebar {
float: right;
width: 200px;
margin-right: -200px;
background-color: #FFA;
}
#cleared {
clear: both;
}
<div id="wrapper">
<div id="content">Column 1 (fluid)</div>
<div id="sidebar">Column 2 (fixed)</div>
<div id="cleared"></div>
</div>