Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add a line in HTML

Understanding the Basics

Before we dive into the specifics, it is crucial to grasp a few fundamental concepts. Let's imagine HTML as a construction kit that you'd use as a kid, where each block (or in this case, an HTML element) has a specific function and fits into a certain place. Just like building a house with blocks, we use these HTML elements to build a webpage.

What is a Line in HTML?

In HTML, a 'line' usually refers to a horizontal rule, a visual line that we use to separate content. It's like the drawn line you would see in a notebook that helps to distinguish between different subjects or topics. In HTML, we use the <hr> tag to create this horizontal rule or line.

The <hr> Tag

The <hr> tag stands for 'horizontal rule'. It is an empty tag, meaning it does not need a closing tag. The <hr> tag creates a line that goes across the viewport, providing a thematic break in the content. Here's an example:

<p>This is some text.</p>
<hr>
<p>This is some text after the horizontal line.</p>

In the above code, the <hr> tag creates a line between two paragraphs of text. When you run this code, you'll see a line separating the two sentences - a clear visual distinction.

Styling the <hr> Tag

We can also modify the line's appearance using CSS (Cascading Style Sheets). CSS acts like a paintbrush, allowing us to alter the color, width, and even the style of the line. Let's take a look at how to do this:

<style>
hr {
    border: none;
    height: 2px;
    color: #333;
    background-color: #333;
}
</style>

<p>This is some text.</p>
<hr>
<p>This is some text after the stylized horizontal line.</p>

In the above code, we have styled the <hr> tag using CSS within the <style> tags. The CSS rules are applied within the curly brackets {}. The border: none; rule removes the default border around the line. The height: 2px; rule sets the line's thickness to 2 pixels. Finally, the color: #333; and background-color: #333; rules change the line's color to a dark grey.

Adding a Line Break

Sometimes, you may want to add a break or a new line without a visible line. For example, you may want to separate two lines of an address or a poem. In HTML, we use the <br> tag for this purpose. The <br> tag stands for 'break', and similar to the <hr> tag, it's also an empty tag.

Here's an example of how to use the <br> tag:

<p>This is the first line.<br>This is the second line.</p>

In this code, the <br> tag creates a new line within the paragraph, separating the text 'This is the first line.' from 'This is the second line.'.

Conclusion

Adding a line or a line break in HTML is straightforward once you understand the purpose and usage of the <hr> and <br> tags. Like the lines in a notebook, these tags help us organize and separate our content, making it easier for our webpage visitors to read and digest the information.

Remember, HTML is like a construction kit. Each element, including the <hr> and <br> tags, has its own role. By understanding these roles and using these elements appropriately, you can create well-structured and visually appealing webpages. Happy coding!