空白 nowrap css 适用于子项,但不适用于父项

发布于 2024-09-28 00:55:02 字数 150 浏览 1 评论 0原文

我有一个里面有多个跨度的 div。跨度包含无法拆分为多行的文本,因此我设置了空白:nowrap。这确实会正确停止每个子范围内的换行,但也会阻止父 div 换行子范围,因此所有范围都位于延伸到页面之外的单行上。

如何更改此行为,以便 nowrap 仅适用于每个范围的内容?

I have a div with multiple spans inside. The spans contain text that cannot be split over multiple lines so I have set whitespace: nowrap. This does stop the wrapping within each child span correctly but also stop the parent div from wrapping the children so all spans are on a single line that stretches off the page.

How do I change this behaviour so the nowrap only applies to the contents of each span?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

要走干脆点 2024-10-05 00:55:02

选项 1

<style>
   .nowrap { white-space: nowrap; }
</style>

<div>
   <span class="nowrap">span 1</span>
   <span class="nowrap">span 2</span>
   <span class="nowrap">span 3</span>
   <span class="nowrap">span 4</span>
   <span class="nowrap">span 5</span>
</div>

选项 2

<style>
   .myDiv span{ white-space: nowrap; }
</style>

<div class="myDiv">
   <span>span 1</span>
   <span>span 2</span>
   <span>span 3</span>
   <span>span 4</span>
   <span>span 5</span>
</div>

Option 1

<style>
   .nowrap { white-space: nowrap; }
</style>

<div>
   <span class="nowrap">span 1</span>
   <span class="nowrap">span 2</span>
   <span class="nowrap">span 3</span>
   <span class="nowrap">span 4</span>
   <span class="nowrap">span 5</span>
</div>

Option 2

<style>
   .myDiv span{ white-space: nowrap; }
</style>

<div class="myDiv">
   <span>span 1</span>
   <span>span 2</span>
   <span>span 3</span>
   <span>span 4</span>
   <span>span 5</span>
</div>
小矜持 2024-10-05 00:55:02
div#id-of-parent span {
    whitespace: nowrap;
}

那应该可以解决问题。 (你的 div 需要 id 属性。)

div#id-of-parent span {
    whitespace: nowrap;
}

That should do the trick. (your div will need the id attribute on it.)

一身仙ぐ女味 2024-10-05 00:55:02

span 元素设置为 display:inline-block

Set the span elements to have display:inline-block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文