Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a line break in HTML

Understanding the Concept of Line Breaks

When you're writing text in a word processor, hitting the 'Enter' key will cause a line break, creating a new line for you to continue your writing. In the realm of HTML, we have a special tag that does the same job - it's called the 'br' tag.

What is the 'br' Tag?

In simple terms, the 'br' tag in HTML is equivalent to pressing 'Enter' on your keyboard. It adds a line break in your text, moving the text following the 'br' tag to the next line. The 'br' tag is one of the few HTML tags that doesn't need to be closed. In other words, you just write <br> and not <br></br>. This is because the 'br' tag is what we call a 'self-closing' or 'void' element in HTML.

Confused? Don't be. Imagine you're writing a letter (yes, a good old-fashioned letter) and you want to start a new paragraph. You'd leave a line blank, right? The 'br' tag does the same thing but in your web page.

Here's an example:

<p>This is a line of text.<br>
This is the second line of text.</p>

If you run this code in your browser, you'll see that the text "This is the second line of text" will appear on a new line.

When to Use the 'br' Tag?

The 'br' tag is handy, but it should be used sparingly and only when necessary. It's like the chili sauce of HTML - a little can add flavor to your web page, but too much can make it messy.

The 'br' tag is mainly used for line breaks in poems or addresses, where the line separation is part of the content.

<p>123 Main Street<br>
Springfield, IL 12345</p>

In this example, the 'br' tag is used to separate the street address from the city and zip code, which is how addresses are typically formatted.

The 'br' Tag vs. Other HTML Tags

You might be wondering, "Why can't I just use another 'p' tag or a 'div' tag to start a new line?" Good question!

While it's true that you can use 'p' or 'div' tags to separate text blocks, they don't function the same way as the 'br' tag. The 'p' and 'div' tags create new blocks of content, complete with their own margins and padding. The 'br' tag, on the other hand, just creates a new line within the same block of content.

Think of it like this: using 'p' or 'div' tags is like starting a new paragraph in your letter, while using the 'br' tag is like just hitting 'Enter' to start a new line within the same paragraph.

Conclusion: The 'br' Tag in a Nutshell

In summary, the 'br' tag in HTML is a simple yet powerful tool that allows you to add line breaks in your text, just like hitting 'Enter' on your keyboard. While it should be used sparingly, it can be very useful for formatting content like addresses or poems where line breaks are part of the content.

Remember, HTML is like the scaffolding of your web page. Just as a builder uses different tools for different tasks, so too should you use the right HTML elements for the right job. The 'br' tag is just one tool in your HTML toolbox, but understanding how and when to use it can help you build better, more readable web pages.

Happy coding!