Home >Web Front-end >CSS Tutorial >How Can I Prevent Long Text from Overflowing Its Container in HTML?

How Can I Prevent Long Text from Overflowing Its Container in HTML?

Barbara Streisand
Barbara StreisandOriginal
2024-12-04 15:24:11891browse

How Can I Prevent Long Text from Overflowing Its Container in HTML?

Word Wrapping Dilemma in HTML

Problem:
Long stretches of text like "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" can exceed the confines of designated containers, creating a fragmented display. This issue arises when text goes beyond the predefined width of a div, leading to unsightly and unreadable content.

Solution:

Via CSS:

The CSS property known as "word-wrap" offers a straightforward solution. By setting it to "break-word," you instruct the browser to break words that extend beyond the specified container's width. This ensures that text flows seamlessly within the designated space.

Sample Code:

div {
    width: 200px;
    word-wrap: break-word;
}

By incorporating this code, your text will automatically wrap within the bounds of the div, preserving its readability and allowing it to adjust to different window sizes and device orientations.

The above is the detailed content of How Can I Prevent Long Text from Overflowing Its Container in HTML?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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