CSS hyphens
Palavras-chave:
Publicado em: 17/08/2025CSS Hyphens: Controlling Line Breaks in Text
The CSS hyphens
property controls how words are split and hyphenated when text wraps across multiple lines. This article explores how to use the hyphens
property to improve the readability and aesthetics of your web pages.
Fundamental Concepts / Prerequisites
To understand the hyphens
property, you should be familiar with basic CSS concepts like text layout, word breaking, and HTML structure. No advanced knowledge is required, but an understanding of how CSS properties cascade and apply to elements will be helpful.
Core Implementation/Solution: Using the hyphens
Property
The hyphens
property can be set to one of three values:
none
: Words are not hyphenated, even if they overflow the container.manual
: Words are only hyphenated if there are explicit hyphenation points (using the soft hyphen HTML entity­
).auto
: The browser automatically inserts hyphens where appropriate, based on its internal dictionary and hyphenation rules. This is the most common and useful setting.
/* HTML: A really long word that needs to be hyphenated automatically.
*/
.hyphenated {
width: 200px; /* Constrain the width to force wrapping */
border: 1px solid black; /* Add a border for visual clarity */
padding: 10px;
hyphens: auto; /* Enable automatic hyphenation */
}
/* Using manual hyphens */
/* HTML: Super­cali­fragi­listic­expi­ali­docious
*/
/* CSS: hyphens: manual; on the container or body */
body {
hyphens: manual;
}
Code Explanation
The CSS code defines a class called hyphenated
. This class sets a fixed width (200px) and a border to visually contain the text. The crucial part is hyphens: auto;
, which tells the browser to automatically hyphenate words if they reach the end of the line and need to wrap. The second example demonstrates using ­
entities for manual hyphenation; this would need the CSS hyphens: manual;
to be enabled on the parent container (or the `body` for global effect) to work.
Complexity Analysis
The hyphens
property itself doesn't introduce significant time or space complexity. The browser's hyphenation algorithm runs automatically when rendering text. The performance impact is generally negligible, especially for modern browsers. The space complexity is also minimal, as it doesn't require storing large amounts of data beyond the text itself and the CSS rules.
Alternative Approaches
An alternative approach is to use JavaScript to implement custom hyphenation logic. Libraries like Hyphenator.js can be used for more fine-grained control over hyphenation. However, this introduces a dependency on JavaScript, increases page load time (slightly), and could potentially impact performance, especially on older browsers or devices. Using the native CSS hyphens
property is almost always the preferred solution unless extremely specific hyphenation rules are required.
Conclusion
The CSS hyphens
property offers a simple and effective way to control word hyphenation in web pages. Using hyphens: auto;
is generally the best approach for improving readability without sacrificing performance. Understanding the different values of the property and when to use manual hyphenation provides greater control over text layout and visual aesthetics.