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

发布于 2024-10-09 10:43:27 字数 467 浏览 6 评论 0原文

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

当我只使用不带样式的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

澉约 2024-10-16 10:43:27

这是工作示例:

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

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>

夜还是长夜 2024-10-16 10:43:27

浮动会使事情变得混乱。通常,要使用浮动,您还需要一个宽度。它不能使它们彼此浮动,因为它不知道每个跨度相对于 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.

挽梦忆笙歌 2024-10-16 10:43:27
<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' 使容器消失,使该元素的子元素成为 DOM 中的下一个级别,我认为这是正确的答案。

另一种在 ie 上也有效的方法是:

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

<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.

Another way which works on ie too is this:

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

杯别 2024-10-16 10:43:27

正是左侧的浮动导致它位于不同的行上。也许可以尝试在跨度之间添加   (不间断空格)。

It's the float left that is causing it to be on separate lines. Maybe try a   (non breaking space) in between the spans.

夜还是长夜 2024-10-16 10:43:27

在某些情况下, display:inline 不起作用,在这种情况下,请尝试在一个父跨度中添加两个跨度,如下所示

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

In some cases display:inline does not work, in such case try adding both spans in one parent span like below

<span>
  <span id="span1">Span 1</span>
  <span id="span2">Span 2</span>
</span>
征棹 2024-10-16 10:43:27

也许溢出?

Overflow maybe?

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

百变从容 2024-10-16 10:43:27

floatdisplay 样式是互斥的,因此没有必要将它们一起使用。另外 默认为 display:inline; 所以这无论如何都是多余的(除非您在其他地方有其他样式将它们设置为其他样式?)。

The float and display styles are mutually exclusive, so there's no point using them together. Also <span> defaults to display:inline; so that's redundant anyway (unless you have some other style elsewhere setting them to something else?).

青春如此纠结 2024-10-16 10:43:27
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>
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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文