How to Wrap Text in HTML?
Last Updated :
25 Oct, 2024
To wrap text in HTML, you can use the default behaviour of block-level elements like <p>, <div>, and <h1>, which automatically wrap text to fit within their parent container. For more control over text wrapping, you can use CSS properties like word-wrap, white-space, and overflow-wrap to determine how the text behaves inside an element.
Using Block-Level Elements
The simplest way to wrap text in HTML is to place the text inside a block-level element, such as <p>, <div>, or <h1>. These elements automatically handle text wrapping based on the width of their parent container.
HTML
<div>
<p>This paragraph will automatically wrap text to the next line
if it doesn't fit within the width of the container.</p>
</div>
Output: The <p> element automatically wraps the text inside it when the content exceeds the width of the container.
Wrap Text in HTMLAlternative Approaches to Wrap Text
Using the <pre> Tag
By using this approach you can control text wrapping in HTML using CSS. By setting white-space: nowrap, the text remains on a single line without wrapping. In contrast, using word-wrap: break-word allows the text to wrap within the container when it exceeds the width.
Syntax
white-space: nowrap; - Prevents text from wrapping.
word-wrap: break-word; - Allows text to wrap, breaking long words if needed.
Example: Example of wrapping text in HTML, with two boxes: one without text wrapping and one with text wrapping.
HTML
<h3>GeeksforGeeks</h3>
<div class="box nowrap">
<p>
This is a long line of
text that will not wrap inside the box.
</p>
</div>
<div class="box wrap">
<p>
This is a long line of text
that will wrap inside the box.
</p>
</div>
CSS
/*style.css*/
.box {
border: 2px solid black;
width: 200px;
height: 100px;
margin: 10px;
padding: 10px;
}
.nowrap {
/* Prevent text from wrapping */
white-space: nowrap;
/* Hide overflow */
overflow: hidden;
}
.wrap {
word-wrap: break-word;
/* Enable text wrapping */
}
Output
How to Wrap Text in HTMLUsing the white-space CSS Property
The white-space property controls how white spaces inside an element are handled, and it can be used to enable text wrapping.
Syntax
.text-wrap {
white-space: normal;
}
Example: Enabling text wrapping using the white-space property.
HTML
<style>
.text-wrap {
white-space: normal;
width: 250px;
border: 1px solid black;
}
</style>
<body>
<h3>GeeksforGeeks</h3>
<p class="text-wrap">
This paragraph will automatically
wrap the text within the specified width.
The white-space property allows text
wrapping.
</p>
</body>
Output
How to Wrap Text in HTML
Similar Reads
How to Underline Text in HTML? Here are two methods to underline text in HTML.1. Underlining text using <u> TagThe <u> tag is used to underline text content. It wraps the content to be underlined with a horizontal line beneath it. This tag is commonly used for hyperlinks or emphasized text.Syntax<u> Underlined T
1 min read
How to Align Text in HTML? In earlier versions of HTML, the align attribute was commonly used to align text to the left, right, or center. However, this attribute is now deprecated in HTML5 and should no longer be used in modern web development. Instead, text alignment is handled more effectively using CSS with the text-align
2 min read
How to move a text in HTML ? To move a text in HTML refers to changing its position within the document's layout structure. This can be achieved by adjusting HTML elements' attributes, such as style or class, and the <marquee> tag to modify their positioning, or by dynamically manipulating the HTML structure using JavaScr
2 min read
Word-wrap in an HTML Table To enable word-wrap in an HTML table, apply the CSS property word-wrap: break-word; to the table cells (<td> or <th> elements). This ensures that long words or strings are broken into multiple lines within the table cell, preventing them from overflowing and maintaining readability.Table
5 min read
How To Edit Text in HTML Paragraph? To edit the text in an HTML paragraph, you can do it by directly editing the HTML file or dynamically using JavaScript or by using contenteditable Attributes. Directly Editing the HTML FileThe simplest way to change the text of a paragraph is to edit it directly in the HTML file. This is useful when
3 min read