HTML <hr> Tag: Usage, Attributes, and Examples

By Cristian G. Guasch •  Updated: 09/25/23 •  11 min read

Diving right into the heart of HTML, let’s discuss a simple yet powerful tool in our coding arsenal – the <hr> tag. This unassuming little bit of code plays a significant role in structuring and styling our web pages. It’s not flashy, but it’s one of those workhorse elements that often flies under the radar.

The <hr> tag is used to draw a horizontal line on your webpage. This might sound mundane, but it’s a key player when you’re looking to break up sections of content or draw attention to specific areas. In fact, I’d wager there’s more versatility and potential in this humble tag than you might initially think.

So, let’s roll up our sleeves and get down to business as we delve into how to use the <hr> tag effectively, exploring its various attributes and showcasing some practical examples along the way. By the end of this piece, you’ll be wielding the <hr> tag like an HTML wizard!

Understanding the HTML <hr> Tag

Diving straight into our topic, let’s shed some light on the HTML <hr> tag. This particular tag is a self-closing one, meaning it doesn’t require a closing tag (</hr>). It stands for ‘horizontal rule’ and it’s primarily used to create a thematic break or horizontal line in an HTML page.

What makes this tag interesting? Well, back in the early days of web design, it was commonly used as a visual tool for separating sections of web content. However, with the evolution of CSS and advanced layout techniques, its usage has shifted more towards semantic purposes.

<!DOCTYPE html>
<html>
<body>

<h1>This is my Heading</h1>
<p>This is my paragraph.</p>

<hr>

<h2>This is another Heading</h2>
<p>This is another paragraph.</p>

</body>
</html> 

In this example above, I’ve positioned <hr> between two distinct sets of headings and paragraphs. The result? A clear demarcation that visually separates these different sections.

But here’s where things get even more intriguing: The <hr> tag also supports several attributes that can modify its appearance! For instance:

Be mindful though! These attributes are not supported in HTML5; they were deprecated due to their presentation-centric nature. Modern web development encourages separation of concerns – handling structure and semantics with HTML while leaving presentation and styling tasks to CSS.

Here’s an example showing how we might use CSS instead:

<!DOCTYPE html>
<html>
<head>
<style>
.hrstyle {
    border-top: 1px solid #8c8b8b;
    width: 50%;
    text-align: left;
}
</style>
</head>
<body>

<h2>A Heading</h2>
<p>Some text..</p>
<hr class="hrstyle">

<h2>Another Heading</h2>
<p>Some other text..</p>

</body>
</html> 

In this example, I’ve assigned a class .hrstyle to the <hr> tag and then styled it using CSS. This approach helps maintain code cleanliness and flexibility.

Common mistakes? Often, developers forget that <hr> is a self-closing tag and unintentionally add a closing </hr> tag. It’s also worth noting that misuse of deprecated attributes can cause inconsistencies across different browsers.

So there you have it – a closer look at the versatile HTML <hr> tag. Whether you’re creating thematic breaks or crafting stylish horizontal lines, understanding how to use this tool effectively will certainly up your web development game!

Key Attributes of the HTML <hr> Tag

Diving right into it, let’s explore some key attributes commonly associated with the HTML <hr> tag. This nifty little element is more versatile than one might initially think.

First off, there’s the color attribute. It’s used to set the color of the horizontal rule line. You’ve probably seen this in action before – a subtle dash of color can really make a website pop! Here’s an example:

<hr color="red">

This code snippet would create a horizontal rule with a vibrant red hue. However, it’s worth noting that this attribute isn’t supported in HTML5 anymore.

Next up is size. This attribute allows you to adjust the thickness of your line by specifying its height in pixels. For instance:

<hr size="4">

With that command, I’ve just created a horizontal rule 4 pixels thick. One thing to keep in mind though: like color, size also isn’t supported in HTML5.

Now onto width. A crucial aspect for layout design and overall aesthetics, width sets how wide your line will be relative to its containing element or as an absolute value (in pixels). Still confused? Check out these examples:

<hr width="50%"> <!-- Relative -->

<hr width="200"> <!-- Absolute -->

The first example makes a horizontal rule that takes up half of its container’s width while the second gives us one with an absolute width of 200 pixels!

One common mistake people often make is neglecting CSS when working with <hr>. It provides greater control over styling and compatibility across different versions of HTML.

And lastly – don’t forget about align. Though not supported under HTML5 standards either, align was once used to position our horizontal rules left, right or center within their containing elements.

All things considered, knowing your attributes can be invaluable when working with HTML tags. With a bit of practice, you’ll start using <hr> like a pro in no time!

Practical Examples of Using HTML <hr> Tag

Diving right into the practicality, let’s explore a few real-world examples of the HTML <hr> tag in action. This tag, as you may recall, is used to create horizontal lines on web pages. It’s an easy way to visually separate different sections of content.

Let’s start with a basic usage example:

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

In this simple bit of code, we’ve got two paragraphs separated by a horizontal line created using the <hr> tag. What if we want to customize that line a bit? That’s where attributes come into play.

Take color for instance. The color attribute isn’t officially supported anymore but it still works in most browsers:

<hr color="red">

This will produce a red horizontal line instead of the default black one. However, since it’s not officially supported, I’d recommend using CSS instead if you need colored lines.

Now let’s add some spice with CSS:

<style>
.hr_example {
    border: none;
    height: 1px;
    background-color: black;
}
</style>

<hr class="hr_example">

Here we’ve defined a CSS class hr_example and assigned it to our <hr> tag. This gives us more control over appearance than just using attributes alone.

While working with <hr> tags, common mistakes might be forgetting that it’s self-closing or trying to nest other elements within it—both won’t work! For example:

<!-- Don't do this -->
<hr><p>Some text</p></hr>

<!-- Do this instead -->
<hr>
<p>Some text</p>

The first example attempts to wrap a paragraph within an <hr> tag which is invalid HTML. The second example correctly places the paragraph below a horizontal line.

These are just a few examples of how to use the <hr> tag effectively in your web designs. Remember, it’s all about creating visual separation and improving readability for your audience.

Common Mistakes and Fixes with HTML <hr>

Diving right into the world of HTML can be a thrilling experience. But, just like any other programming language, it’s not without its pitfalls. From misunderstanding syntax to misusing attributes, the mistakes are plentiful. Let’s explore some common blunders made when using the HTML <hr> tag and learn how to rectify them.

One frequent faux pas involves forgetting to close the <hr> tag properly. Many beginners often treat it like a traditional opening-closing pair (i.e., <hr></hr>). However, according to HTML5 standards, this isn’t correct. The <hr> tag is a self-closing element; you simply use it as <hr /> or even just <hr>.

<!-- WRONG -->
<hr></hr>

<!-- RIGHT -->
<hr />
or
<hr>

Next up on our list of common pitfalls is overuse of the width attribute in an attempt to adjust horizontal lines’ length. While it might seem intuitive to use width for this purpose, keep in mind that this attribute has been deprecated in HTML5! To alter your line’s length now, leverage CSS instead.

<!-- WRONG -->
<hr width="50%">

<!-- RIGHT --> 
<hr style="width:50%;">

Another usual mistake I’ve noticed involves attempting to influence the thickness of an <hr> line by changing its height via CSS properties like border-width or height. This doesn’t work because these properties don’t apply here. Instead, utilize the border property for size adjustments.

/* WRONG */
.hr {
  border-width: 2px;
}

/* RIGHT */
.hr {
  border: none;
  border-top: 2px solid black;
}

Lastly, don’t forget about color! It’s easy to overlook but can make a big difference in your design. Many people change the color of an <hr> line using the color attribute – but this is outdated and unsupported in HTML5. Instead, use CSS properties such as border-color.

/* WRONG */
.hr {
  color: red;
}

/* RIGHT */
.hr {
  border-color: red;
}

While these are just a few examples, they highlight some commonly overlooked aspects when working with HTML’s <hr> tag. By keeping these tips in mind and continuing to learn and experiment, you’ll master the usage of this versatile element before you know it!

Concluding Thoughts on Effective Use of HTML <hr> Tag

I’ve dived deep into the world of HTML <hr> tags, shining a light on its uses, attributes, and examples. Now, I’ll share my final thoughts.

Understanding and effectively using the <hr> tag can significantly enhance your webpage’s structure and readability. It’s not just about drawing lines but creating meaningful separations in your content. The simplicity of this tag makes it easy to overlook its potential value.

Remember:

Let me show you an example:

<h2>Section 1</h2>
<p>This is some content.</p>

<hr style="height:2px;border-width:0;color:gray;background-color:gray">

<h2>Section 2</h2>
<p>This is some more content.</p>

Here, we’re separating two sections with a gray horizontal line with height set at 2px.

A common mistake I often see is the misuse or overuse of this element for aesthetic purposes only. It’s crucial to remember that while it does add visual breaks, its primary function lies in semantic structuring.

Also, keep in mind that self-closing syntax (<hr/>) isn’t necessary in HTML5. So avoid using it unless you’re writing XHTML code.

In conclusion, mastering the HTML <hr> tag isn’t rocket science. With a solid grasp on its semantics and careful implementation, you can leverage this simple yet mighty tool to create well-structured web pages that are both visually pleasing and easily navigable.

Cristian G. Guasch

Hey! I'm Cristian Gonzalez, I created HTML Easy to help you learn HTML easily and fast.

Related articles