html - 阻止包含 span 的单词换行?

标签 html css word-wrap

我在使用包含 span 的换行词时遇到一些问题。

下面的代码看起来相当复杂,但它所做的只是向“imagination”一词中的所有 i 添加彩色点。

<h2>The&nbsp;Clore&nbsp;Prize&nbsp; <span style="text-decoration: underline; color: rgb(255, 211, 0);">i</span>mag<span style="text-decoration: underline; color: rgb(0, 179, 223);">i</span>nat<span style="text-decoration: underline; color: rgb(238, 65, 34);">i</span>on&nbsp;lab</h2>

我遇到的唯一问题是单词在跨度处换行。我怎样才能阻止它这样做呢?我不想改变添加彩色点的方式。

这就是发生的事情:

enter image description here

如果我在 h2 上不加换行,那么它会执行以下操作(单词会从屏幕上消失):

enter image description here

已开启 this page

最佳答案

删除单词中的所有空格,或使用 white-space: nowrap:

<h2>These words will wrap. <span style="white-space:nowrap"><span style="text-decoration: underline; color: rgb(255, 211, 0);">i</span>longwordlongwordlongwordlongwordlongwordlongwordlongwordlongword<span style="text-decoration: underline; color: rgb(0, 179, 223);">i</span>nat<span style="text-decoration: underline; color: rgb(238, 65, 34);">i</span>on</span> Wrap</h2>

一般来说,最好将 CSS 与 HTML 分开,但这看起来像是一次性代码,您不太可能在文档的其他地方重用,因此在这里使用样式属性内联它可能是合适的。

关于html - 阻止包含 span 的单词换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34769620/

相关文章:

javascript - 带有附加模糊或焦点事件的聚焦元素在第二次单击时会触发它

html - 如何在桌面和移动设备上制作相同物理尺寸的按钮?

jquery - 第 n 个 child 。每第 4/5 个、第 9/10 个、第 14/15 个元素为目标

css-grid 和 `fr` 单元不适用于 `overflow-wrap: break-word`

python - wxPython ListCtrl 在调整大小时换行

html - 换行时引入元素宽度?

html - 四个div并排响应

html - <div> 元素而不是 <table>?

javascript - 从 dom 中隐藏输入值属性

jquery - Bootstrap 导航栏似乎比实际内容长