简体   繁体   English

如何使用 CSS 将两个跨度包装成一行

[英]How to wrap two spans into one line with CSS

I want to wrap two spans in one line with CSS.我想用 CSS 将两个spans包装在一行中。

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.我想使用“id”,使用divspan ,我只希望它们在一行中。

When I just use span without style, the content are not in the same line.当我只使用没有样式的span时,内容不在同一行。 The second line will go down.第二行将下降。

Here is the working example:这是工作示例:

 <div style="float:left;"> <span style="display:inline; color: red;">First Span</span> <span style="display:inline; color: blue;">Second Span</span> </div>

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.它不能将它们相互浮动,因为它不知道每个跨度相对于 div 将占用多少空间。 Spans are inherently inline elements unless you define them otherwise, so they should just display that way without the float.跨度本质上是内联元素,除非您以其他方式定义它们,因此它们应该在没有浮动的情况下以这种方式显示。

 <div style="float:left;"> <span style="display:contents; color: red;">First Span</span> <span style="display:contents; color: blue;">Second Span</span> </div>

'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. 'display:contents' 使容器消失,使子元素的子元素在 DOM 中更上一层楼,我相信这是正确的答案。

Another way which works on ie too is this:另一种适用于 ie 的方法是:

 <div style="float:left; display:flex;"> <span style="color: red;">First Span</span> <span style="color: blue;">Second Span</span> </div>

In some cases display:inline does not work, in such case try adding both spans in one parent span like below在某些情况下display:inline不起作用,在这种情况下,请尝试在一个父跨度中添加两个跨度,如下所示

<span>
  <span id="span1">Span 1</span>
  <span id="span2">Span 2</span>
</span>

It's the float left that is causing it to be on separate lines.是左浮动导致它位于不同的行上。 Maybe try a &nbsp;也许试试&nbsp; (non breaking space) in between the spans. (非破坏空间)在跨度之间。

可能溢出?

<div style="width:60px; overflow:hidden;">

The float and display styles are mutually exclusive, so there's no point using them together. floatdisplay样式是互斥的,所以没有必要一起使用它们。 Also <span> defaults to display:inline; <span>默认为display:inline; so that's redundant anyway (unless you have some other style elsewhere setting them to something else?).所以无论如何这都是多余的(除非您在其他地方有其他风格将它们设置为其他风格?)。

You can use Table.
    
<table>
    <tbody>
        <tr>
            <td><span>Span 1 text</span></td>
            <td><span>Span 2 text</span></td>
        </tr>
        <tr>
            <td><span>Span 3 text</span></td>
            <td><span>Span 4 text</span></td>
        </tr>
    </tbody>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM