Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a horizontal line in HTML

Understanding the Basics

Before we dive deep into creating a horizontal line in HTML, let's ensure we have a firm grip on some fundamental concepts. In the world of web development, HTML, which stands for HyperText Markup Language, is the backbone of any webpage.

Imagine you're creating your dream house. HTML is like the bricks and mortar that give structure to your house. Similarly, HTML provides structure to a webpage.

The Horizontal Line - Breakdown

In our house analogy, imagine you have different rooms for different activities. To distinguish one room from another, you use some form of partition. Similarly, in the world of HTML, you might want to separate different sections of your webpage for clarity. This is where the horizontal line comes into play. A horizontal line in HTML is a way to visually separate different sections of a webpage.

The HR Tag

Creating a horizontal line in HTML is simple. HTML provides a dedicated tag for this purpose: the <hr> tag. The <hr> tag stands for 'horizontal rule'. This tag does not require any closing tag. This means you don't need to add </hr> after using <hr>.

Here's an example of how you might use it:

<p>This is some text.</p>
<hr>
<p>This is some more text.</p>

When you run this code, you will see two blocks of text, separated by a horizontal line.

Styling the Horizontal Line

Like many other HTML elements, the horizontal line can also be styled using CSS (Cascading Style Sheets). CSS is the interior designer of our house analogy. It applies colors, layouts, and designs to the structure provided by HTML.

Let's say we want to change the color of the horizontal line to red. Here's how you can do it:

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

<p>This is some text.</p>
<hr>
<p>This is some more text.</p>

In this example, we've used the <style> tag to include some CSS. The hr inside the curly brackets {} is a CSS selector. It means that the following styles will be applied to all <hr> elements on the page.

Within the curly brackets, we've defined some properties:

  • border: none; - removes the default border around the horizontal line.
  • height: 2px; - sets the thickness of the line.
  • color: red; and background-color: red; - changes the color of the line.

Making a Dotted Line

Perhaps you want something a bit more stylish than a plain line. How about a dotted line? Here's how you can achieve that:

<style>
  hr.dotted {
    border-top: 1px dotted red;
  }
</style>

<p>This is some text.</p>
<hr class="dotted">
<p>This is some more text.</p>

In this example, we've added a class to our <hr> tag. A class in HTML is a way to select specific elements that you want to style. We've named our class 'dotted'.

In our CSS, we've changed our selector to hr.dotted. This means that the following styles will only be applied to <hr> elements with the class 'dotted'.

border-top: 1px dotted red; creates a top border that is 1 pixel wide, dotted, and red.

Conclusion

Just like different rooms in a house, different sections of a webpage need to be partitioned for clarity. The <hr> tag in HTML provides an easy way to create this partition in the form of a horizontal line.

Styling this line allows you to get creative with your partitions and make your webpage more visually appealing. With a little bit of CSS, you can change the color, thickness, and even the pattern of your horizontal line.

So, whether you want a thin, thick, dotted, or rainbow-colored line, the power is in your hands. Happy coding!