This is first Line<br>This is second line
This is first Line
This is line one <br>This is line two after one line break<br><br>line three after two line breaks<br>
The output is here
This is line one
This is line two after one line break
line three after two line breaks
While the <br> tag is commonly used for line breaks, CSS offers alternative methods that provide greater control over text formatting:
Example:
<style>
.pre-line {
white-space: pre-line;
}
</style>
<p class="pre-line">
This is the first line.
This is the second line with preserved spacing.
</p>
The <pre> tag allows us to display text exactly as it is written in the HTML, preserving both spaces and line breaks. This is particularly useful for displaying code snippets or formatted text.
Example:
<pre>
Line one
Indented line two
Line three
</pre>
When dealing with user-generated content or text from databases that include newline characters (e.g., "\n"), we can use CSS to render these line breaks in HTML:
<style>
.formatted-text {
white-space: pre-wrap;
}
</style>
<div class="formatted-text">
This is user-generated content.
Each new line is preserved.
</div>
Understanding and implementing line breaks effectively enhances the readability and structure of web content. By utilizing both HTML tags and CSS properties, we can achieve precise control over text formatting, leading to better user experiences and more maintainable code.
HTML Paragraph in HTML pages Bold Tag