如何使用 CSS 并排浮动 3 个 div?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2156712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:29:40  来源:igfitidea点击:

How to float 3 divs side by side using CSS?

csscss-float

提问by Dameon

I know how to make 2 divs float side by side, simply float one to the left and the other to the right.

我知道如何让 2 个 div 并排浮动,只需将一个向左浮动,另一个向右浮动。

But how to do this with 3 divs or should I just use tables for this purpose?

但是如何用 3 个 div 来做到这一点,或者我应该为此目的使用表格?

回答by Nick Craver

Just give them a width and float: left;, here's an example:

只需给它们一个宽度和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>

回答by Stickers

The modern way is to use the CSS flexbox, see support tables.

现代方法是使用CSS flexbox,请参阅支持表

.container {
  display: flex;
}
.container > div {
  flex: 1; /*grow*/
}
<div class="container">
  <div>Left div</div>
  <div>Middle div</div>  
  <div>Right div</div>
</div>

You can also use CSS grid, see support tables.

您还可以使用CSS 网格,请参阅支持表

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr; /* fraction*/
}
<div class="container">
  <div>Left div</div>
  <div>Middle div</div>  
  <div>Right div</div>
</div>

回答by Sarfraz

It is same way as you do for the two divs, just float the third one to left or right too.

这与您对两个 div 的处理方式相同,只需将第三个向左或向右浮动即可。

<style>
  .left{float:left; width:33%;}
</style>

<div class="left">...</div>
<div class="left">...</div>
<div class="left">...</div>

回答by davidosomething

float them all left

把它们都留在左边

make sure a width is specified that they can all fit in their container (either another div or the window), otherwise they will wrap

确保指定了一个宽度,它们都可以适合它们的容器(另一个 div 或窗口),否则它们将被包裹

回答by Migisha

<br style="clear: left;" />

that code that someone posted up there, it did the trick!!! when i paste it just before closing the Container DIV, it helps clear all subsequent DIVs from overlapping with the DIVs i've created side-by-side at the top!

有人在那里发布的代码,它成功了!!!当我在关闭容器 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>

tadaa!! :)

多啊!!:)

回答by Arwen

Float all three divs to the left. Like here:

将所有三个 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;
}

回答by SugaComa

<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>

the advantage of this way is you can set each column width independant of the other as long as you keep it under 100%, if you use 3 x 30% the remaining 10% is split as a 5% divider space between the collumns

这种方式的优点是您可以将每个列的宽度设置为彼此独立,只要您将其保持在 100% 以下,如果您使用 3 x 30%,则剩余的 10% 将被拆分为 5% 的列之间的分隔空间

回答by Nick Travers

I usually just float the first to the left, the second to the right. The third automatically aligns between them then.

我通常只是将第一个浮动到左边,第二个浮动到右边。然后第三个会自动在它们之间对齐。

<div style="float: left;">Column 1</div>
<div style="float: right;">Column 3</div>
<div>Column 2</div>

回答by Daniel A. White

you can float: left for all of them and set the width to 33.333%

您可以 float: left 为所有这些并将宽度设置为 33.333%

回答by Ann

try to add "display: block" to the style

尝试在样式中添加“显示:块”

<style>
   .left{
          display: block;
          float:left; 
          width:33%;
    }
</style>


<div class="left">...</div>
<div class="left">...</div>
<div class="left">...</div>