Home > Web Front-end > CSS Tutorial > How Can I Prevent Long Words from Breaking My DIV Layout?

How Can I Prevent Long Words from Breaking My DIV Layout?

Barbara Streisand
Release: 2024-12-09 01:09:11
Original
544 people have browsed it

How Can I Prevent Long Words from Breaking My DIV Layout?

Preventing Long Words from Breaking Your Div

In the switch from table-based layouts to DIV-based layouts, a common problem persists: the unsightly spillover of long words over the edge of DIV columns. This issue plagues even major websites and is particularly prevalent in languages like German, where even everyday words can be lengthy.

Solution:

Soft Hyphen

Employing the soft hyphen (­) enables you to designate where browsers should break long words:

averyvery­longword
Copy after login

This character allows browsers to render the word either as "averyverylongword" or "averyvery-

longword."

A regular expression can assist in strategically inserting soft hyphens:

/([^\s-]{5})([^\s-]{5})/ → ­
Copy after login

Element

Alternatively, inject the HTML5 element, a former IE-ism:

averyvery<wbr>longword
Copy after login

This breaks the word without a hyphen, resulting in "averyvery

longword."

CSS Hyphens

Supported by the latest versions of IE, Firefox, and Safari (not Chrome), CSS hyphens enable automatic hyphenation:

div.breaking {
  hyphens: auto;
}
Copy after login

Note that this feature relies on a hyphenation dictionary and may not always break long words consistently.

Overflow and White-space: pre-wrap

Other viable solutions include controlling overflow and setting white-space to pre-wrap:

div.breaking {
  overflow: hidden;
  white-space: pre-wrap;
}
Copy after login

Both approaches prevent long words from extending past the boundary of the DIV.

The above is the detailed content of How Can I Prevent Long Words from Breaking My DIV Layout?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template