如何使用 CSS 将两个 Span 包装成一行
我想用 CSS 将两个 span
包装在一行中。
这是我的代码:
<div style="width:60px;">
<span id="span1" style="float:left; display:inline;"></span>
<span id="span2" style="float:left; display:inline; "></span>
</div>
但它不起作用。
怎么做呢?
编辑:
我想使用“id”,使用div
或span
,我只想它们在一行中。
当我只使用不带样式的 span
时,内容不在同一行。第二行将会下降。
I want to wrap two spans
in one line with CSS.
Here is my code:
<div style="width:60px;">
<span id="span1" style="float:left; display:inline;"></span>
<span id="span2" style="float:left; display:inline; "></span>
</div>
But it doesn't work.
How to do that?
Edit:
I want to use the "id", either use div
or span
, I just want them to be in one line.
When I just use span
without style, the content are not in the same line. The second line will go down.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是工作示例:
Here is the working example:
浮动会使事情变得混乱。通常,要使用浮动,您还需要一个宽度。它不能使它们彼此浮动,因为它不知道每个跨度相对于 div 占用多少空间。 Span 本质上是内联元素,除非您以其他方式定义它们,因此它们应该以这种方式显示而无需浮动。
The float will mess things up. Usually with a float to work you need a width with it as well. It can't float them against each other because it doesn't know how much space each span will occupy in relation to the div. Spans are inherently inline elements unless you define them otherwise, so they should just display that way without the float.
'display:contents' 使容器消失,使该元素的子元素成为 DOM 中的下一个级别,我认为这是正确的答案。
另一种在 ie 上也有效的方法是:
'display:contents' Makes the container disappear, making the child elements children of the element the next level up in the DOM which I believe is the right answer.
Another way which works on ie too is this:
正是左侧的浮动导致它位于不同的行上。也许可以尝试在跨度之间添加
(不间断空格)。
It's the float left that is causing it to be on separate lines. Maybe try a
(non breaking space) in between the spans.
在某些情况下,
display:inline
不起作用,在这种情况下,请尝试在一个父跨度中添加两个跨度,如下所示In some cases
display:inline
does not work, in such case try adding both spans in one parent span like below也许溢出?
Overflow maybe?
<div style="width:60px; overflow:hidden;">
float
和display
样式是互斥的,因此没有必要将它们一起使用。另外默认为
display:inline;
所以这无论如何都是多余的(除非您在其他地方有其他样式将它们设置为其他样式?)。The
float
anddisplay
styles are mutually exclusive, so there's no point using them together. Also<span>
defaults todisplay:inline;
so that's redundant anyway (unless you have some other style elsewhere setting them to something else?).