html - CSS:连字符:自动(如果无法自动换行)

标签 html css

在下拉菜单中,我有两种不同的情况:

一个菜单项有多个单词,它应该在一个单词后中断。 另一个菜单项有一个长单词,它应该用自动连字符分隔

white-space: pre-line;
word-wrap: break-word;

结果:

This item breaks
like it should

ThisItemDoesNotBre
akLikeItShould

这会按其应有的方式打破多单词元素,并打破菜单元素末尾的单单词元素。添加一个

hyphens:auto

使单字项看起来很棒,但多字项也会在单词内部中断,而不是像以前一样在单词末尾处中断:

This item doesn't bre-
ak like it should

ThisItemDoesBreak-
LikeItShould

是否可以使 hyphens:auto 仅发生在那些在单词后不能中断的菜单项上?

最佳答案

您使用的 CSS 规则根据单词的长度而不是根据一个长单词中的“单词”来中断单词。

只有当浏览器知道您正在使用哪种语言并且有可用的字典时,它才会知道在正确的位置断开单词。

Hyphenation rules are language-specific. In HTML, the language is determined by the lang attribute, and browsers will hyphenate only if this attribute is present and if an appropriate hyphenation dictionary is available. On XML, the xml:lang attribute must be used. Source

如果每个单词都以大写字母开头,您可以尝试使用 Javascript/JQuery 分隔长单词。

关于html - CSS:连字符:自动(如果无法自动换行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38950355/

相关文章:

javascript - 如何停止带有 li 元素的非常基本的 slider 中的无限幻灯片?

html - 更改 Shiny App 的背景颜色-- CSS

javascript - 在 div 中获取一个 carousal table 值

jquery - 在两个不同的部分 想传递两个变量

html - 图像不会在样式或 img 本身中调整大小

CSS 下拉菜单 : nav ul ul li Moved to Right

CSS 下拉导航,第 3 级问题

CSS 规则阴影并不总是有效

css - div 缺少填充

html - 使用文本对齐将 HTML 表格导出为 PDF