
CSS Blend Modes: How to Mix Overlapping Colors
06/05/2024
CSS Filter Property: Image and Container Effects
08/05/2024
Long words can overflow a narrow column or leave large gaps at the ends of lines. The CSS hyphens property lets you control whether a browser can divide a word at a line break.
1. How CSS hyphens handles long words
Hyphenation gives the browser additional line-break opportunities within words. That can help a narrow column fit its text more comfortably. Compare the examples below.
Compare hyphenation settings in CodePen
The two containers illustrate how changing hyphens affects the same text:
- Without automatic hyphenation: The browser still wraps at ordinary break opportunities such as spaces. A word too long for the line may overflow unless another CSS rule allows it to break.
- With automatic hyphenation:
hyphens: autolets the browser use rules for the text’s language to find suitable breaks within words, where a dictionary is available.
2. Syntax and values of hyphens
hyphens controls whether words can be hyphenated when wrapping text.
Syntax:
.hyphenation {
hyphens: auto | manual | none;
}
Values:
- auto: Allows automatic hyphenation according to the text’s language and the browser’s available rules.
- manual: Only allows explicit hyphenation opportunities, for example a soft hyphen (
­). - none: Disables hyphenation. Other wrapping rules can still allow a break.
Example:
Use the following declaration for automatic hyphenation:
.hyphenation {
hyphens: auto;
}
The browser can then choose where to hyphenate a word when it has suitable language rules.
3. Set the text language for correct hyphenation
Use the HTML lang attribute to tell the browser the language of the text. Different languages have different rules for where words can be divided.
In Spanish, for example, the word teléfono can be split as te-/léfono, telé-/fono, or teléfo-/no. The split follows the word’s syllables.
For a page written in Spanish, set its root language as follows:
<html lang="es">
This lets the browser select Spanish hyphenation rules when they are available.
4. Content in a different language from the page
If a quote or paragraph uses a different language, set lang on the element that contains it. For example, label an English paragraph inside a Spanish document with lang="en".
<p lang="en">This is an example sentence in English within a paragraph.</p>
The browser can then apply the rules appropriate to each fragment rather than treating every word as if it were in the page’s main language.
5. Practical examples
5.1. Hyphenation in cards
The card has an image, an h3 title, and a paragraph. Both text elements use hyphens: auto, allowing longer words to wrap inside a narrow card.
5.2. Hyphenation with line-clamp
hyphens controls word breaks within a line. Line clamping limits how many lines are visible in the preview.
This card shows up to three lines and allows automatic hyphenation when needed. Provide an accessible way to reach the complete text.
6. Browser support
Check browser support for CSS hyphens and test the languages your site uses. Support for the property does not guarantee a dictionary for every language.
Set the document language, then compare the results at narrow widths. When necessary, use an additional wrapping rule for words that the browser cannot hyphenate.
For more web development lessons, follow the Eniun newsletter.