Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make a line in HTML

Understanding the Basics

Before we dive into the nitty-gritty of how to make a line in HTML, it's essential to understand the basics of HTML. In simple terms, HTML (Hyper Text Markup Language) is the backbone of any web page. It's like the skeleton that gives your web page structure.

Now, let's get started!

What is a Line in HTML?

In HTML, a line is a horizontal divider that separates content in your webpage. Think of it like a physical line drawn on a piece of paper that separates different sections. In HTML, we use a specific tag to create this line: the <hr> tag. The "hr" stands for "horizontal rule."

Here's how it looks in code:

<hr>

This simple tag, when placed into an HTML document, will create a horizontal line on your website.

Using the <hr> Tag

The <hr> tag is a standalone tag, which means it doesn't need to be closed. This is a bit like a door that remains open and doesn't need to be closed. You'll place the <hr> tag where you want your line to appear in your HTML document.

Here's an example of how it might be used:

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

In this example, the <hr> tag creates a line between two paragraphs of text.

Customizing Your Line

The best part about the <hr> tag is that it's customizable. You can adjust the line's width, height (thickness), and color to fit the aesthetic of your website.

To adjust the width of the line, you use the width attribute in the <hr> tag. The value can be either an absolute size (like "200px" for 200 pixels) or a relative size (like "50%" for half the width of its containing element).

Here's an example:

<hr width="50%">

In this example, the horizontal line will take up 50% of the width of its containing element.

To adjust the height (or thickness) of the line, you use the size attribute. Just like the width attribute, the size attribute's value can be specified in pixels.

Here's an example:

<hr size="2">

In this example, the horizontal line's thickness will be 2 pixels.

Lastly, to change the line color, you'll need to use CSS (Cascading Style Sheets). CSS is like the clothes and accessories that make your HTML skeleton look good. For the <hr> tag, you can use the border-color property to change the line color.

Here's an example:

<hr style="border-color: #FF0000;">

In this example, the horizontal line will be red.

A Note on Style and Usage

While the <hr> tag is a handy tool, it's important to note that it should be used sparingly. The main purpose of a line in HTML is to denote a thematic break in content. It's not meant to be used for styling purposes. For more complex style requirements, CSS is the tool to use.

As a best practice, always use the <hr> tag semantically — to separate different topics or ideas in your content, much like how chapters in a book are separated.

Wrapping Up

The <hr> tag is a simple yet powerful tool in HTML. By understanding how to use and style it, you can create more structured and readable content on your webpages. Remember, while the <hr> tag is handy for creating visual breaks, it's best used to denote thematic breaks in your content. Happy coding!