Preventing Long Words from Breaking Your Div
In the transition from table-layout to div-layout, one enduring issue persists: the unwelcome extension of excessively long words beyond the boundaries of a div column. This occurrence can compromise the aesthetics and professionalism of a website, as witnessed frequently even on prominent sites, particularly those featuring languages with inherently lengthy terms.
Solution 1: Soft Hyphen
Inserting a soft hyphen () within long words allows browsers to determine appropriate break points:
averyvery­longword
This results in rendering as:
averyverylongword
or
averyvery- longword
Solution 2:
Alternatively, the
averyvery<wbr>longword
This breaks the word cleanly at the break point:
averyvery longword
Solution 3: CSS Hyphens (Unsupported by Chrome)
CSS hyphens (enabled by hyphens: auto;) automatically hyphenate words based on a dictionary. While it can enhance the appearance of justified text, it's not a reliable solution for preventing long words from exceeding div boundaries.
Retrospective Solution: display: table-cell
Despite its deprecation for layout purposes, the display: table-cell; property can still be applied to other elements, emulating the stretchy behavior of table cells, which dynamically expand to accommodate long words.
The above is the detailed content of How Can I Prevent Long Words from Breaking Out of My Divs?. For more information, please follow other related articles on the PHP Chinese website!