To insert a horizontal line in HTML, use the <hr> element:
<hr>
The <hr> element creates a horizontal rule that represents a thematic break between sections of content. It does not require a closing tag because <hr> is a void element.
For example:
<h2>Introduction</h2>
<p>This is the first section of the page.</p>
<hr>
<h2>Conclusion</h2>
<p>This is the second section of the page.</p>
This places a horizontal line between the two sections.
What Is the <hr> Element in HTML?
<hr> stands for horizontal rule. In modern HTML, its meaning is more specific than simply drawing a line. It represents a thematic separation between paragraphs, sections, or changes in topic.
For example:
<p>HTML defines the structure of a webpage.</p>
<hr>
<p>CSS controls the appearance of the webpage.</p>
The browser displays a horizontal separator between the two paragraphs.
The <hr> element is normally the correct HTML element when you need a semantic horizontal separation in content.
How to Change the Horizontal Line With CSS
The default <hr> appearance depends on the browser. You can use CSS to control its color, width, thickness, margins, and style.
For example:
<hr class="separator">
.separator {
width: 80%;
border: 0;
border-top: 2px solid #000;
}
This creates a horizontal line that is 80% of its containing element’s width and has a 2-pixel solid black border.
How to Change the Color
Use the border-color property or define the border directly.
hr {
border: 0;
border-top: 2px solid #333;
}
You can use any valid CSS color:
hr {
border-top: 2px solid red;
}
Or:
hr {
border-top: 2px solid #0077cc;
}
Removing the default border with border: 0 gives you predictable results across browsers before adding your own border.
How to Change the Thickness
The thickness of the line can be controlled with the border width:
hr {
border: 0;
border-top: 5px solid black;
}
Here, the horizontal line is 5 pixels thick.
You can also use other CSS units:
hr {
border: 0;
border-top: 0.2rem solid black;
}
Using border-top is generally preferable to setting a fixed height because the line remains an actual separator rather than becoming a generic block with a background.
How to Change the Width
You can make the horizontal line shorter or longer using the width property:
hr {
width: 50%;
border: 0;
border-top: 2px solid black;
}
You can also use a fixed width:
hr {
width: 400px;
border: 0;
border-top: 2px solid black;
}
A percentage is useful when the line should scale with its container.
How to Center a Horizontal Line
If the <hr> has a width smaller than its container, you can center it with automatic margins:
hr {
width: 60%;
margin: 30px auto;
border: 0;
border-top: 2px solid black;
}
The auto value on the left and right margins centers the element horizontally.
How to Add Space Above and Below the Line
Use CSS margin to control the space around the horizontal rule:
hr {
margin: 30px 0;
border: 0;
border-top: 1px solid #ccc;
}
This adds 30 pixels of vertical space above and below the line.
You can also set the margins separately:
hr {
margin-top: 20px;
margin-bottom: 40px;
border: 0;
border-top: 1px solid #ccc;
}
How to Create a Dashed or Dotted Horizontal Line
CSS can change the border style.
Dashed line:
hr {
border: 0;
border-top: 2px dashed #333;
}
Dotted line:
hr {
border: 0;
border-top: 2px dotted #333;
}
Common border styles include solid, dashed, and dotted.
Can You Use <hr /> Instead?
You may also see:
<hr />
In HTML5, the slash is unnecessary. The standard form is:
<hr>
Both may be accepted by browsers, but <hr> is the normal HTML5 syntax.
Should You Use CSS Instead of <hr>?
Use <hr> when the line represents a meaningful separation or thematic break in your content.
For example:
<h2>Product Details</h2>
<p>Product information...</p>
<hr>
<h2>Shipping Information</h2>
<p>Shipping details...</p>
If you only need a decorative design element, CSS or another appropriate layout element may be more suitable.
Avoid creating a horizontal line by typing repeated characters such as:
-------------------------
That creates text rather than a proper semantic separator and can behave poorly across different screen sizes and fonts.
Complete Example
Here is a complete HTML example with a customized horizontal line:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Horizontal Line Example</title>
<style>
hr {
width: 70%;
margin: 30px auto;
border: 0;
border-top: 2px solid #333;
}
</style>
</head>
<body>
<h1>First Section</h1>
<p>This is the content of the first section.</p>
<hr>
<h2>Second Section</h2>
<p>This is the content of the second section.</p>
</body>
</html>
The essential HTML is simply <hr>. Use CSS when you need to control its appearance, including width, thickness, color, spacing, alignment, or border style. For a semantic horizontal separation between content sections, <hr> is the appropriate HTML element.
Relevant Guide
How To Convert Ipynb To Html
How To Link An Email In Html
How To Open A Link In A New Tab Html
How To Upload Html File To WordPress
How To Add Schema Markup To Html
How To Call A Function In Javascript From Html
How To Center An Iframe In Html
How To Change Plain Text Email To Html
How To Make Text Wrap Around An Image In Html
Are Html And Css Programming Languages
Do My Html Homework
How To Add Alt Tags To Images In Html
