Nic*_*ver 291
只要给它们一个宽度float: left;,这是一个例子:
<div style="width: 500px;">
<div style="float: left; width: 200px;">Left Stuff</div>
<div style="float: left; width: 100px;">Middle Stuff</div>
<div style="float: left; width: 200px;">Right Stuff</div>
<br style="clear: left;" />
</div>
Run Code Online (Sandbox Code Playgroud)
Sti*_*ers 119
现代方法是使用CSS flexbox,请参阅支持表.
.container {
display: flex;
}
.container > div {
flex: 1; /*grow*/
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div>Left div</div>
<div>Middle div</div>
<div>Right div</div>
</div>Run Code Online (Sandbox Code Playgroud)
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* fraction*/
}Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div>Left div</div>
<div>Middle div</div>
<div>Right div</div>
</div>Run Code Online (Sandbox Code Playgroud)
Sar*_*raz 51
它与你对两个div的方式相同,只是向左或向右浮动第三个div.
<style>
.left{float:left; width:33%;}
</style>
<div class="left">...</div>
<div class="left">...</div>
<div class="left">...</div>
Run Code Online (Sandbox Code Playgroud)
Mig*_*sha 22
<br style="clear: left;" />
Run Code Online (Sandbox Code Playgroud)
那个人在那里发布的代码,它就是诀窍!当我在关闭Container DIV之前粘贴它时,它有助于清除所有后续DIV与我在顶部并排创建的DIV重叠!
<div>
<div class="left"></div>
<div class="left"></div>
...
...
<div class="left"></div>
<!-- then magic trick comes here -->
<br style="clear: left;" />
</div>
Run Code Online (Sandbox Code Playgroud)
tadaa!:)
Arw*_*wen 15
向左浮动所有三个div.像这儿:
.first-div {
width:370px;
height:150px;
float:left;
background-color:pink;
}
.second-div {
width:370px;
height:150px;
float:left;
background-color:blue;
}
.third-div {
width:370px;
height:150px;
float:left;
background-color:purple;
}
Run Code Online (Sandbox Code Playgroud)
小智 9
我通常只是将第一个浮动到左侧,第二个浮动到右侧.第三个然后自动在它们之间对齐.
<div style="float: left;">Column 1</div>
<div style="float: right;">Column 3</div>
<div>Column 2</div>
Run Code Online (Sandbox Code Playgroud)
小智 9
<style>
.left-column
{
float:left;
width:30%;
background-color:red;
}
.right-column
{
float:right;
width:30%;
background-color:green;
}
.center-column
{
margin:auto;
width:30%;
background-color:blue;
}
</style>
<div id="container">
<section class="left-column">THIS IS COLUMN 1 LEFT</section>
<section class="right-column">THIS IS COLUMN 3 RIGHT</section>
<section class="center-column">THIS IS COLUMN 2 CENTER</section>
</div>
Run Code Online (Sandbox Code Playgroud)
这种方式的优点是你可以设置每个列宽度独立于另一个,只要你保持在100%以下,如果使用3 x 30%,剩下的10%被分割为柱子之间的5%分隔空间
我没有看到引导程序的答案,所以它的价值是:
<div class="col-xs-4">Left Div</div>
<div class="col-xs-4">Middle Div</div>
<div class="col-xs-4">Right Div</div>
<br style="clear: both;" />
Run Code Online (Sandbox Code Playgroud)
让 Bootstrap 计算出百分比。我喜欢清除两者,以防万一。
小智 6
尝试在样式中添加"display:block"
<style>
.left{
display: block;
float:left;
width:33%;
}
</style>
<div class="left">...</div>
<div class="left">...</div>
<div class="left">...</div>
Run Code Online (Sandbox Code Playgroud)
小智 5
我更喜欢这种方法,旧版本的 IE 对浮动的支持很差(真的吗?...)
.column-left{ position:absolute; left: 0px; width: 33.3%; background: red; }
.column-right{position:absolute; left:66.6%; width: 33.3%; background: green; }
.column-center{ position:absolute; left:33.3%; width: 33.3%; background: yellow; }
Run Code Online (Sandbox Code Playgroud)
更新:当然,要使用此技术,并且由于绝对定位,您需要将 div 包含在容器上并进行后处理以定义 if 的高度,如下所示:
jQuery(document).ready(function(){
jQuery('.main').height( Math.max (
jQuery('.column-left').height(),
jQuery('.column??-right').height(),
jQuery('.column-center').height())
);
});
Run Code Online (Sandbox Code Playgroud)
这不是世界上最神奇的东西,但至少不会在旧的 IE 上崩溃。