hyphens

Baseline 2023 *
Newly available

Since September 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

* Some parts of this feature may have varying levels of support.

CSS 属性 hyphens 告知浏览器在换行时如何使用连字符连接单词。可以完全阻止使用连字符,也可以控制浏览器什么时候使用,或者让浏览器决定什么时候使用。

连字规则具有语言特定性。在 HTML 中,语言由 lang 属性决定,浏览器只会在当前属性存在且有合适的连字字典可用的情况使用连字进行连接。在 XML 中,必须使用 xml:lang 属性。

备注: 在规范中,没有明确定义连字符的实现规则,所以具体的连字符在不同浏览器中可能有所区别。

语法

css
hyphens: none;
hyphens: manual;
hyphens: auto;

/* Global values */
hyphens: inherit;
hyphens: initial;
hyphens: unset;

none

即便单词内有建议换行点也不会在那里换行。只会在空白符处换行。

manual

只有当单词内存在建议换行点时,才会在该位置断开单词并使用连字符换行。查看建议换行点了解详情。

auto

The browser is free to automatically break words at appropriate hyphenation points, following whatever rules it chooses to use. Suggested line break opportunities, as covered in Suggesting line break opportunities, should be preferred over automatically selecting break points whenever possible.

备注: auto 的行为取决于语言是否被正确地设置,以便可以选择适当的断词规则。你必须使用 HTML 属性 lang 指定语言,以确保自动断词在你选择的语言中得到应用。

建议换行点

有两个 Unicode 字符可以用于在文本中手动指定可能的换行点:

U+2010(HYPHEN)

“硬”连字符表示一个可见的换行点。即使在指定的位置没有真正换行,连字符仍然会显示出来。

U+00AD(SHY)

一个不可见的“软”连字符。此字符不会在屏幕上显示出来,而是表示在必要时浏览器可能会在该位置断开单词并出现连字符。在 HTML 中,可以使用 ­ 来插入软连字符。

形式定义

初始值manual
适用元素所有元素
是否是继承属性
计算值as specified
动画类型离散值

形式语法

hyphens = 
none |
manual |
auto

示例

以下代码段展示了 hyphens 属性取 none/manual/auto 这三类值的效果。

html
<dl>
  <dt><code>none</code>: no hyphen; overflow if needed</dt>
  <dd lang="en" class="none">An extreme&shy;ly long English word</dd>
  <dt>
    <code>manual</code>: hyphen only at &amp;hyphen; or &amp;shy; (if needed)
  </dt>
  <dd lang="en" class="manual">An extreme&shy;ly long English word</dd>
  <dt><code>auto</code>: hyphens where the algorithm decides (if needed)</dt>
  <dd lang="en" class="auto">An extreme&shy;ly long English word</dd>
</dl>
css
dd {
  width: 55px;
  border: 1px solid black;
}
dd.none {
  hyphens: none;
}
dd.manual {
  hyphens: manual;
}
dd.auto {
  hyphens: auto;
}

规范

Specification
CSS Text Module Level 3
# hyphens-property

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
hyphens
auto value
Hyphenation dictionary for Afrikaans (af, af-*)
Hyphenation dictionary for Albanian (sq, sq-*)
Hyphenation dictionary for Ahmaric (am, am-*)
Hyphenation dictionary for Armenian (hy, hy-*)
Hyphenation dictionary for Assamese (as, as-*)
Hyphenation dictionary for Basque (be, be-*)
Hyphenation dictionary for Belarusian (be, be-*)
Hyphenation dictionary for Bengali (bn, bn-*)
Hyphenation dictionary for Bosnian, Serbian, and Serbo-Croatian (sh, sh-*, sr, sr-*, bs, bs-*)
Hyphenation dictionary for Bulgarian (bg, bg-*)
Hyphenation dictionary for Catalan (ca, ca-*)
Hyphenation dictionary for Croatian (hr, hr-*)
Hyphenation dictionary for Mongolian (Cyrillic) (mn-cyrl, mn-cyrl-*)
Hyphenation dictionary for Czech (cs, cs-*)
Hyphenation dictionary for Danish (da, da-*)
Hyphenation dictionary for Dutch (nl, nl-*)
Hyphenation dictionary for English (en, en-*)
Hyphenation dictionary for Esperanto (eo, eo-*)
Hyphenation dictionary for Estonian (et, et-*)
Hyphenation dictionary for Ethiopic script (mul-ethi, mul-ethi-*)
Hyphenation dictionary for Ethiopic script (und-ethi, und-ethi-*)
Hyphenation dictionary for Finnish (fi, fi-*)
Hyphenation dictionary for French (fr, fr-*)
Hyphenation dictionary for Galician (gl, gl-*)
Hyphenation dictionary for Georgian (ka, ka-*)
Hyphenation dictionary for German, Reformed Orthography of 1996 (de, de-1996, de-DE, de-AT, de-*)
Hyphenation dictionary for German, Swiss Orthography (de-CH, de-CH-*)
Hyphenation dictionary for German, Traditional Orthography of 1901 (de-1901, de-AT-1901, de-DE-1901)
Hyphenation dictionary for Gujarati (gu, gu-*)
Hyphenation dictionary for Hindi (hi, hi-*)
Hyphenation dictionary for Hungarian (hu, hu-*)
Hyphenation dictionary for Icelandic (is, is-*)
Hyphenation dictionary for Interlingua (ia, ia-*)
Hyphenation dictionary for Irish (ga, ga-*)
Hyphenation dictionary for Italian (it, it-*)
Hyphenation dictionary for Kannada (kn, kn-*)
Hyphenation dictionary for Kurmanji (kmr, kmr-*)
Hyphenation dictionary for Latin (la, la-*)
Hyphenation dictionary for Latvian (lv, lv-*)
Hyphenation dictionary for Lithuanian (lt, lt-*)
Hyphenation dictionary for Malayalam (ml, ml-*)
Hyphenation dictionary for Marathi (mr, mr-*)
Hyphenation dictionary for Modern Greek (el, el-*)
Hyphenation dictionary for Mongolian (mn, mn-*)
Hyphenation dictionary for Norwegian (Nynorsk) (nn, nn-*)
Hyphenation dictionary for Norwegian (Bokmål) (no, no-*, nb, nb-*)
Hyphenation dictionary for Old Slavonic (cu, cu-*)
Hyphenation dictionary for Oriya (or, or-*)
Hyphenation dictionary for Polish (pl, pl-*)
Hyphenation dictionary for Portuguese (pt, pt-*)
Hyphenation dictionary for Punjabi/Panjabi (pa, pa-*)
Hyphenation dictionary for Russian (ru, ru-*)
Hyphenation dictionary for Slovak (sk, sk-*)
Hyphenation dictionary for Slovenian (sl, sl-*)
Hyphenation dictionary for Spanish (es, es-*)
Hyphenation dictionary for Swedish (sv, sv-*)
Hyphenation dictionary for Tamil (ta, ta-*)
Hyphenation dictionary for Telugu (te, te-*)
Hyphenation dictionary for Turkish (tr, tr-*)
Hyphenation dictionary for Turkmen (tk, tk-*)
Hyphenation dictionary for Ukrainian (uk, uk-*)
Hyphenation dictionary for Upper Sorbian (hsb, hsb-*)
Hyphenation dictionary for Welsh (cy, cy-*)

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
See implementation notes.
Requires a vendor prefix or different name for use.
Has more compatibility info.

参见