我知道如何让2个divs并排浮动,简单地浮动一个到左边,另一个到右边。
但是如何用3个div来做这个,或者我应该只用表来做这个目的吗?
我知道如何让2个divs并排浮动,简单地浮动一个到左边,另一个到右边。
但是如何用3个div来做这个,或者我应该只用表来做这个目的吗?
当前回答
<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>
这种方法的优点是你可以设置每一列的宽度,只要你保持在100%以下,如果你使用3 x 30%,剩下的10%被分割为5%的分隔空间列之间
其他回答
把它们都飘到左边
确保所指定的宽度都能适合它们的容器(无论是另一个div还是窗口),否则它们将自动换行
display: table;如果文本需要显示,如果在同一行
换句话说;如果在每个<div>文本的垂直对齐需要是相同的,一个可以尝试现代复古回到过去的有点有争议的表格样式:
.container {display: table;}
div {display: table-cell;}
事实证明,这对于在Pandoc中格式化csl风格的引用非常有用,如下所示:
div.csl-bib-body {}
div.csl-entry {
margin-top: 1rem;
display: table;
}
div.csl-left-margin {
display: table-cell;
}
div.csl-right-inline {
padding-left: 1ex;
display: table-cell;
}
引文编号div和引文数据div现在显示在完全相同的高度。
下面是我如何在<footer>元素中做类似的事情:
<div class="content-wrapper">
<div style="float:left">
<p>© 2012 - @DateTime.Now.Year @Localization.ClientName</p>
</div>
<div style="float:right">
<p>@Localization.DevelopedBy <a href="http://leniel.net" target="_blank">Leniel Macaferi</a></p>
</div>
<div style="text-align:center;">
<p>☎ (24) 3347-3110 | (24) 8119-1085 ✉ @Html.ActionLink(Localization.Contact, MVC.Home.ActionNames.Contact, MVC.Home.Name)</p>
</div>
</div>
CSS:
.content-wrapper
{
margin: 0 auto;
max-width: 1216px;
}
现代的方法是使用CSS flexbox,参见支持表。
.container { 显示:flex; } .container > div { flex: 1;/ * * / } < div class = "容器" > < div >左div < / div > 中产div < div > < / div > < div >正确的div < / div > < / div >
也可以使用CSS网格,参见支持表格。
.container { 显示:网格; Grid-template-columns: 1fr 1fr 1fr;/ * * / } < div class = "容器" > < div >左div < / div > 中产div < div > < / div > < div >正确的div < / div > < / div >
你可以为它们设置float: left,并将宽度设置为33.333%