如何将三个div放在同一行?

85

有人能帮我解决这个问题吗?我已经处理了很长时间了...

我正在尝试将3个div放在同一行,其中一个div看起来像这样:

<div>  
  <h2 align="center">San Andreas: Multiplayer</h2>  
  <div align="center">
    <font size="+1">  
      <em class="heading_description">15 pence per slot</em>  
    </font>  
    <img src="http://fhers.com/images/game_servers/sa-mp.jpg" class="alignleft noTopMargin" style="width: 188px; ">  
    <a href="gfh" class="order-small">  
      <span>order</span></a>  
  </div>

另外两个是相同的div,请帮我把这三个div放在同一行,一个在右边,一个在中间,一个在左边。


  1. 欢迎来到 Stack Overflow。
  2. 通过换行、缩进等方式格式化您的 HTML 代码会让它更易于阅读。
  3. 您需要更清楚地说明您的需求。这些元素应该填满页面宽度还是使用它们的默认尺寸?
- vdbuilder
4
我为格式进行了编辑。对于新手要温柔一些,他们粘贴了HTML并应用了代码格式,这在我看来对于一个1级新手来说相当不错。 - Michael Durrant
1
@Michael Durrant 没错,作为第一篇帖子还不错。 - vdbuilder
我可以建议在涉及HTML的问题时,您可以使用jsfiddle(除了在此处显示之外)来演示问题。 - Marcin
请不要发布答案,除非它们回答了问题,请使用评论功能 :) - Tim Post
显示剩余2条评论
12个回答

150

我很惊讶没有人提出CSS表格布局作为解决方案:

.Row {
    display: table;
    width: 100%; /*Optional*/
    table-layout: fixed; /*Optional*/
    border-spacing: 10px; /*Optional*/
}
.Column {
    display: table-cell;
    background-color: red; /*Optional*/
}
<div class="Row">
    <div class="Column">C1</div>
    <div class="Column">C2</div>
    <div class="Column">C3</div>
</div>

兼容 IE8+

查看JSFiddle演示


14
也许是因为有人认为“表格不好”。对于标记中的非表格数据,这是正确的。但我们现在谈论的是样式。将一个元素的样式设置得像表格一样并没有什么问题。我点了赞。 :-) - Malax
.Row > div { display: table-cell; background-color: red; } - George Norberg
@GeorgeNorberg 是的,你可以这样做。 - avrahamcool
1
请注意,在 table-cell 的 DIV 上可能需要添加%的宽度,以便将它们显示为您想要的方式:http://stackoverflow.com/questions/43565213/how-to-distribute-multiple-divs-sections-within-same-row - HappyHands31
可以固定列的高度并显示滚动条吗? - D T
显示剩余3条评论

41

查看我的代码

.float-left {
    float:left;
    width:300px; // or 33% for equal width independent of parent width
}
<div>
    <h2 align="center">San Andreas: Multiplayer</h2>
    <div align="center" class="float-left">CONTENT OF COLUMN ONE GOES HERE</div>
    <div align="center" class="float-left">CONTENT OF COLUMN TWO GOES HERE</div>
    <div align="center" class="float-left">CONTENT OF COLUMN THREE GOES HERE</div>
</div>


我用了这个...#leftcolumn { float: left; width: 300px; } #midcolumn { margin: 0 auto; width: 300px; } #rightcolumn { float: right; width: 300px; }但是所有东西都搞乱了网站...而且中间也不起作用... - Mr.Blue
float: left; width: 300px; 放置在 #leftcolumn、#midcolumn 和 #rightcolumn 中。 - Gabriel Santos
1
它不会把所有东西都设置为向左移动吗? - Mr.Blue
所有的元素都向左移动,但第一个 div 的宽度为 300 像素,因此第二个从左侧开始距离为 300 像素,第三个为 600 像素,因此第一个在左侧,第二个在中间,第三个在右侧。 - Gabriel Santos

32

我不确定为什么会进入这篇文章,但是因为大多数答案都使用浮动、绝对定位和其他现在不再是最优选项的方法,所以我想给出一个更符合标准(浮动不再适用)且更为时新的答案。

.parent {
  display: flex;
  flex-direction:row;
}

.column {
  flex: 1 1 0px;
  border: 1px solid black;
}
<div class="parent">
    <div class="column">Column 1</div>
    <div class="column">Column 2<br>Column 2<br>Column 2<br>Column 2<br></div>
    <div class="column">Column 3</div>
</div>


11

2019年的答案:

使用CSS网格布局:

.parent {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr;
}

3
如果您不知道网格中会有多少元素,可以将“template”规则替换为“grid-auto-flow: column”。 - forresthopkinsa
嘿,感谢您提供最新的CSS答案,不知道您是否可以添加一个使用grid-auto-flow: column的示例,正如评论中提供的那样?当列数未知时,我无法弄清楚正确的使用方法。谢谢。 - ghiscoding

11

这里有两个样例:http://jsfiddle.net/H5q5h/1/

第一个使用了float:left和一个带有overflow:hidden的包装器。该包装器确保包装器的同级元素从包装器下方开始。

第二个使用了较新的display:inline-block,并且可以忽略包装器。但是,这在旧浏览器中通常不受支持,因此请谨慎使用。而且,任何项目元素之间的空格都会导致不必要的左右“margin-like”空白。


很遗憾,目前仍然没有任何内容...... 但是如果您能将HTML代码和CSS粘贴进去,那就太好了...谢谢您。代码从第44行开始:http://pastebin.com/rW5bfjsv,CSS代码在这里:http://pastebin.com/9yrRTYCh。请您把它们放进去并重新上传到pastebin.com,这对我真的非常有帮助,因为我已经尝试了很多次,但都没有结果。谢谢。 - Mr.Blue
哇!那是很多代码。在我的例子中,你只需要用你的 div 替换我的 "item divs",并保留类名。不要忘记包装器。顺便说一句,在 SO 中使用 jsfiddle 是一个好方法。 - Joseph
2
+1 for inline-block。不应该使用浮动布局,而应该用于在文本中浮动图像等。display: inline-block 就是正确的选择。 - Madara's Ghost
行内块在旧版浏览器上存在问题。最终我找到了一个链接,其中说明了这一切:http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/ 这就是为什么我更喜欢使用溢出+左浮动的方法。 - Joseph
很遗憾,仍然没有任何进展。如果您能将它添加到我的代码中,那将非常有帮助,因为我尝试过了,但没有成功...但是,如果您能将HTML代码和CSS粘贴进去,那就太好了...谢谢!从第44行开始:http://pastebin.com/rW5bfjsv 和CSS... http://pastebin.com/9yrRTYCh 请您把它放进去并重新上传到pastebin.com,这将对我非常有帮助,因为我已经尝试了很多次,但没有结果。谢谢! - Mr.Blue

11

虽然是旧话题,但也许有人会喜欢它。

JSFiddle链接http://jsfiddle.net/74ShU/

<div class="mainDIV">
    <div class="leftDIV"></div>
    <div class="middleDIV"></div>
    <div class="rightDIV"></div>
</div>

和 CSS

.mainDIV{
    position:relative;
    background:yellow;
    width:100%;
    min-width:315px;
}
.leftDIV{
    position:absolute;
    top:0px;
    left:0px;
    height:50px;
    width:100px;
    background:red;
}
.middleDIV{
    height:50px;
    width:100px;
    background:blue;
    margin:0px auto;
}
.rightDIV{
    position:absolute;
    top:0px;
    right:0px;
    height:50px;
    width:100px;
    background:green;
}

3

只需在除最后一个外,您想使其在同一行中显示的所有div上添加float left属性。以下是示例:

<div>
  <div style="float: left;">A</div>
  <div style="float: left;">B</div>
  <div>C</div>
</div>

1
为什么不尝试使用Bootstrap的解决方案。如果您不想插手表格和浮动,它们非常完美。

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/> <!--- This line is just linking the bootstrap thingie in the file. The real thing starts below -->

<div class="container">
  <div class="row">
    <div class="col-sm-4">
      One of three columns
    </div>
    <div class="col-sm-4">
      One of three columns
    </div>
    <div class="col-sm-4">
      One of three columns
    </div>
  </div>
</div>

无需干扰复杂的CSS,最好的事情是您可以通过更改数字来编辑列的宽度。您可以在https://getbootstrap.com/docs/4.0/layout/grid/找到更多示例。


1
这是更简单的方法,可以为从未使用过的无序/有序列表标签赋予目的。
在您的CSS中添加:
    li{float: left;}  //Sets float left property globally for all li tags.

然后在你的HTML中添加:


    <ul>
         <li>1</li>
         <li>2</li>
         <li>3</li>        
    </ul>

现在,一切都可以完美地排列!不再争论表格与div的问题!

0
将分区放入“td”标签中。就这样完成了。

请检查此链接:http://jsfiddle.net/pankajparashar/EAzj6/1/。所有部件都在同一行上...正是你所要求的。如果您期望其他内容,请告诉我。 - Pankaj Parashar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接