HTML <br> line break tag
The <br>
tag in HTML stands for "line break." It is used to insert a line break within text, forcing the content following the <br>
tag to start on a new line. This tag is an empty (self-closing) element, meaning it does not have a closing tag.
Syntax:
<p>This is some text.<br>This is on a new line.</p>
Key Characteristics:
Line Break: The primary function of the
<br>
tag is to create a line break within text, which is useful for formatting content where a new line is needed without starting a new paragraph.Empty Tag: As a self-closing tag,
<br>
does not require a closing tag. It is written as<br>
in HTML4 and<br />
in XHTML and HTML5.Text Wrapping: The
<br>
tag affects only the text immediately before and after it. It does not create extra space beyond the line break, unlike block-level elements like paragraphs or headings.
Example:
<p>Line one.<br>Line two.<br>Line three.</p>
In this example:
- The text "Line two" appears on a new line below "Line one."
- The text "Line three" appears on a new line below "Line two."
Use Cases:
Addresses: Useful for formatting addresses where line breaks are needed between lines of the address.
Example:
<address> 123 Main St.<br> Suite 4B<br> Springfield, IL 62704<br> USA </address>
Poetry or Song Lyrics: Ideal for displaying lines of poetry or song lyrics where line breaks are part of the formatting.
Example:
<p> Roses are red,<br> Violets are blue,<br> Sugar is sweet,<br> And so are you. </p>
Forms: Can be used in forms to create line breaks between input fields or labels without additional spacing.
Example:
<label for="name">Name:</label><br> <input type="text" id="name" name="name"><br> <label for="email">Email:</label><br> <input type="email" id="email" name="email">
Limitations:
Accessibility: Overuse of
<br>
tags for layout purposes can make the document less accessible, as screen readers may not interpret them well. It’s generally better to use CSS for layout and spacing.Semantics:
<br>
tags are best used for inline formatting needs rather than structural changes. For more significant content separation, consider using block-level elements like<p>
,<div>
, or<section>
.
Modern Alternatives:
For layout and spacing, CSS is usually a better approach than using <br>
tags. CSS provides more control over spacing, alignment, and overall design, allowing for more flexible and accessible layouts.
Example using CSS:
<style>
.line-break {
display: block;
margin-bottom: 1em;
}
</style>
<p>
Line one.<span class="line-break"></span>
Line two.<span class="line-break"></span>
Line three.
</p>