Line Break in html pages using <br> tag

This is first Line<br>This is second line
This is first Line
This is second line

Line breaks in html pages can be given by using <br> tag. It does not have any end tags and it can be used at any place within the body tags of the page.

Inside table cells and other simple areas also we can use line break tag <br>. More than one break tags can be used to give more line breaks. We will discuss here simple html line breaks and its uses.

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

In a text area if you are using any line breaks then it does not convert on its own to line breaks in html pages. As a result of this many time you will find posting by users or after the form is submitted the page get stretched horizontally without any line breaks. There are different ways to convert the line breaks in html pages, you can see php example of converting line breaks for html web pages.

Using CSS for Line Breaks

While the <br> tag is commonly used for line breaks, CSS offers alternative methods that provide greater control over text formatting:

  • white-space: pre; Preserves both spaces and line breaks as they appear in the HTML code.
  • white-space: pre-line; Collapses multiple spaces but preserves line breaks.
  • white-space: pre-wrap; Preserves both spaces and line breaks, and wraps text as needed.

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>

Using the <pre> Tag for Preformatted Text

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>

Line Breaks in Formatted Content

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>

Best Practices for Using Line Breaks

  • Avoid using <br> tags for separating paragraphs; instead, use <p> tags for better semantics and accessibility.
  • Use CSS for controlling text layout and spacing to maintain separation of content and presentation.
  • Reserve the use of <br> tags for cases where line breaks are semantically meaningful, such as in addresses or poetry.

Conclusion

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


plus2net.com










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer