鉴于此HTML:
<div>foo</div><div>bar</div><div>baz</div>
如何让它们像这样显示内联:
foo bar baz
不喜欢这样:
FOO
酒吧
巴兹
答案 0 :(得分:246)
内联div是网络的狂热者。应该被打败直到它成为一个跨度(至少9次中的10次)......
<span>foo</span>
<span>bar</span>
<span>baz</span>
...回答原来的问题...
答案 1 :(得分:245)
那就是其他的东西:
div.inline { float:left; }
.clearBoth { clear:both; }
<div class="inline">1<br />2<br />3</div>
<div class="inline">1<br />2<br />3</div>
<div class="inline">1<br />2<br />3</div>
<br class="clearBoth" /><!-- you may or may not need this -->
答案 2 :(得分:167)
尝试这样写:
div { border: 1px solid #CCC; }
<div style="display: inline">a</div>
<div style="display: inline">b</div>
<div style="display: inline">c</div>
答案 3 :(得分:32)
已经阅读了这个问题和答案了几次,我所能做的就是假设已经进行了相当多的编辑工作,而我怀疑是因为没有提供足够的答案而给你错误的答案信息。我的线索来自于使用br
标记。
向达里尔道歉。我读了class =“inline”作为style =“display:inline”。即使你使用语义上有问题的类名,你也有正确的答案; - )
错过使用br
提供结构布局而不是文本布局对我来说太普遍了。
如果您希望在divs
内添加多个内联元素,那么您应该将这些内容放在div
上,而不是将它们内联。
Floated divs:
===== ======= == **** ***** ****** +++++ ++++
===== ==== ===== ******** ***** ** ++ +++++++
=== ======== === ******* **** ****
===== ==== ===== +++++++ ++
====== == ======
内联div:
====== ==== ===== ===== == ==== *** ******* ***** *****
**** ++++ +++ ++ ++++ ++ +++++++ +++ ++++
如果你追求的是前者,那么这就是你的解决方案并丢失了br
个标签:
<div style="float: left;" >
<p>block level content or <span>inline content</span>.</p>
<p>block level content or <span>inline content</span>.</p>
</div>
<div style="float: left;" >
<p>block level content or <span>inline content</span>.</p>
<p>block level content or <span>inline content</span>.</p>
</div>
<div style="float: left;" >
<p>block level content or <span>inline content</span>.</p>
<p>block level content or <span>inline content</span>.</p>
</div>
请注意,这些div的宽度是流畅的,因此如果要控制行为,请随意在其上放置宽度。
谢谢, 史蒂夫
答案 4 :(得分:23)
将display:inline-block
与IE6 / 7的边距和媒体查询一起使用:
<html>
<head>
<style>
div { display:inline-block; }
/* IE6-7 */
@media,
{
div { display: inline; margin-right:10px; }
}
</style>
</head>
<div>foo</div>
<div>bar</div>
<div>baz</div>
</html>
答案 5 :(得分:10)
您应该使用
<span>
代替<div>
以获得正确的方法 的内联即可。因为div是块级元素,您的要求是内联块级元素。
根据您的要求,这是html代码:
<div class="main-div">
<div>foo</div>
<div>bar</div>
<div>baz</div>`
</div>
display:inline-block;
float:left;
所以你要强行更改显示属性display:inline-block;
示例一
div {
display: inline-block;
}
示例二
div {
float: left;
}
你需要清除浮动
.main-div:after {
content: "";
clear: both;
display: table;
}
答案 6 :(得分:7)
如前所述,显示:内联可能就是你想要的。有些浏览器也支持内联块。
答案 7 :(得分:7)
只需使用带有“float:left”的包装div,并将包含在其中的框也包含float:left:
CSS:
wrapperline{
width: 300px;
float: left;
height: 60px;
background-color:#CCCCCC;}
.boxinside{
width: 50px;
float: left;
height: 50px;
margin: 5px;
background-color:#9C0;
float:left;}
HTML:
<div class="wrapperline">
<div class="boxinside">Box 1</div>
<div class="boxinside">Box 1</div>
<div class="boxinside">Box 1</div>
<div class="boxinside">Box 1</div>
<div class="boxinside">Box 1</div>
</div>
答案 8 :(得分:6)
<style type="text/css">
div{
position: relative;
display: inline-block;
width:25px;
height:25px;
}
</style>
<div>toto</div>
<div>toto</div>
<div>toto</div>
答案 9 :(得分:5)
<span>
?
答案 10 :(得分:4)
我会使用跨度或向左浮动div。浮动的唯一问题是你必须事后清除浮点数或包含div必须将溢出样式设置为auto
答案 11 :(得分:3)
您需要包含三个div。这是一个例子:
<强> CSS 强>
div.contain
{
margin:3%;
border: none;
height: auto;
width: auto;
float: left;
}
div.contain div
{
display:inline;
width:200px;
height:300px;
padding: 15px;
margin: auto;
border:1px solid red;
background-color:#fffff7;
-moz-border-radius:25px; /* Firefox */
border-radius:25px;
}
注意:border-radius属性是可选的,仅适用于符合CSS3标准的浏览器。
<强> HTML 强>
<div class="contain">
<div>Foo</div>
</div>
<div class="contain">
<div>Bar</div>
</div>
<div class="contain">
<div>Baz</div>
</div>
请注意,div''foo''bar'和'baz'都保存在'contains'div中。
答案 12 :(得分:3)
我知道有人说这是一个糟糕的主意,但如果你想做一些像瓷砖图像那样带有评论的东西,它实际上可能很有用。例如Picasaweb使用它来显示相册中的缩略图 请参阅示例/ demo http://closure-library.googlecode.com/svn/trunk/closure/goog/demos/inline_block_quirks.html(类goog-inline-block;我在此处将其缩写为ib)
/* below is a set of hacks to make inline-block work right on divs in IE. */
html > body .ib { display:inline-block; }
.ib {display:inline-block;position:relative;}
* html .ib { display: inline; }
:first-child + html .ib { display:inline; }
鉴于CSS,将div设置为类ib,现在它神奇地成为内联块元素。
答案 13 :(得分:2)
<style type="text/css">
div.inline { display:inline; }
</style>
<div class="inline">a</div>
<div class="inline">b</div>
<div class="inline">c</div>
答案 14 :(得分:2)
我认为你可以使用这种方式而不使用任何CSS -
<table>
<tr>
<td>foo</td>
</tr>
<tr>
<td>bar</td>
</tr>
<tr>
<td>baz</td>
</tr>
</table>
现在你正在使用块级元素,这样你就会得到不需要的结果。所以你可以内联像span,small等元素。
<span>foo</span><span>bar</span><span>baz</span>
答案 15 :(得分:0)
<div class="cdiv">
<div class="inline"><p>para 1</p></div>
<div class="inline">
<p>para 1</p>
<span>para 2</span>
<h1>para 3</h1>
</div>
<div class="inline"><p>para 1</p></div>
答案 16 :(得分:0)
我们可以这样做
.left {
float:left;
margin:3px;
}
<div class="left">foo</div>
<div class="left">bar</div>
<div class="left">baz</div>
答案 17 :(得分:0)
package math.Shapes;
import java.util.ArrayList;
import java.util.List;
/**
* Driver for Shape3D
* Created by Michael
* Creation date 2/7/2016.
* @link https://stackoverflow.com/questions/35258063/learning-about-inheritance-and-cant-get-my-calculations-to-work
*/
public class DriverShape3D {
public static void main(String[] args) {
List<Shape3D> shapes = new ArrayList<Shape3D>() {{
add(new Cone(1.0, 1.0));
add(new Sphere(1.0));
add(new Cone(5.0, 5.0));
add(new Sphere(5.0));
}};
for (Shape3D shape : shapes) {
System.out.println(shape);
}
}
}
答案 18 :(得分:-1)
我只是倾向于使它们固定宽度,以便它们相加到页面的总宽度 - 可能只有在使用固定宽度页面时才有效。也“漂浮”。