Html 是否可以在不浮动的情况下显示彼此相邻的 div?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3726390/
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 04:31:29  来源:igfitidea点击:

Is that possible to display divs next to each other without floating?

csshtml

提问by Misha Moroshko

I want to put several divs next to each other in one row. All divs have the same height.

我想将几个divs放在一行中。所有divs 都具有相同的高度。

Hereis how this can be done using float: left.

以下是如何使用float: left.

Can this be done without using float?

这可以在不使用的情况下完成float吗?

采纳答案by Ragnis

Depends, on what you want to do. You can use display: inline-block;

取决于你想做什么。您可以使用display: inline-block;

http://jsfiddle.net/sygL9/

http://jsfiddle.net/sygL9/

回答by Palantir

You could use display:inline-block. But unfortunately some browsers (some IE versions) don't support it.

您可以使用 display:inline-block。但不幸的是某些浏览器(某些 IE 版本)不支持它。

http://www.brunildo.org/test/inline-block.html

http://www.brunildo.org/test/inline-block.html

http://www.quirksmode.org/css/display.html

http://www.quirksmode.org/css/display.html

回答by Matt Briggs

a display: blockelement is (effectively) going to have a line break at the end. One option that will let you keep block element styles, while putting it in the pageflow is set display: inline-block(of course, with some additional workto get ie behaving)

一个display: block元素(有效地)最后会有一个换行符。一个选项可以让你保留块元素样式,同时把它放在页面流中display: inline-block(当然,还有一些额外的工作来获得 ie 行为)

Another option is to nest them, set them all to position: relative, and use the leftrule to align them.

另一种选择是嵌套它们,将它们全部设置为position: relative,然后使用left规则对齐它们。

回答by Peter

#together {
    position: absolute;
    display: inline-block;
 margin;left:10px;
 background-color:lightblue;
 width:500px;
 border:4px double blue;
}
<div id="together" style="left:10px">first div</div>
<div id="together" style="left:520px">second div</div>
<div id="together" style="left:1030px">third div</div>
<div id="together" style="left:1540px">fourth div</div>
<div id="together" style="left:2050px">fifth div</div>
<div id="together" style="left:2560px">sixth div</div>
<div id="together" style="left:3070px">seventh div</div>

Here you are, with seven divs in a row

在这里,连续七个 div

回答by bonzi

If you have the possibility to change the divs to spanelements, that would fix the problem without using CSS

如果您可以将 div 更改为元素,则无需使用 CSS 即可解决问题

 <span></span><span></span><span></span><span></span>