Question
I want to change the color of an <hr> element using CSS, but the code below does not seem to work:
hr {
color: #123455;
}
<hr>
How can I correctly change the visible color of the horizontal rule?
Short Answer
By the end of this page, you will understand why styling an <hr> with color often does not produce the expected result, and how to correctly change its appearance using CSS properties such as border, border-color, background-color, and height. You will also see practical examples, common mistakes, and a small project to practice styling separators in a web page.
Concept
The main concept behind this question is how the <hr> element is rendered and styled in CSS.
An <hr> element represents a thematic break between sections of content. Although it looks like a simple line, browsers usually draw it using borders, not regular text color.
That is why this CSS often does not work as expected:
hr {
color: #123455;
}
The color property mainly affects text color. Since <hr> does not contain visible text, changing color usually does not change the visible line.
To style the visible line, developers typically change one of these:
borderborder-colorheightbackground-color
For example:
hr {
border: 1px solid #123455;
}
or:
Mental Model
Think of an <hr> like a divider strip placed on a page.
colorchanges the ink used for text.borderchanges the outline of a box.background-colorchanges the fill inside a box.
An <hr> is usually displayed more like a thin box or border than like text. So if you try to change its color, you are changing the wrong paint bucket.
A better mental model is:
- If you are styling words, use
color - If you are styling a line, check
borderorbackground-color
Syntax and Examples
The most common ways to change an <hr> color are below.
1. Use border-color
hr {
border: 1px solid #123455;
}
This works because the visible line is usually the border.
2. Remove the border and use background-color
hr {
border: none;
height: 2px;
background-color: #123455;
}
This creates a clean custom line with a specific thickness.
3. Style only one side of the border
hr {
border: none;
border-top: 3px solid #123455;
}
This is a very common pattern because it avoids unexpected browser defaults.
Example HTML and CSS
<p>Section one</p>
<hr =>
Section two
Step by Step Execution
Consider this example:
<hr class="line">
.line {
border: none;
height: 4px;
background-color: #123455;
}
Here is what happens step by step:
- The browser creates an
<hr>element. - By default, the browser may give it a built-in border style.
border: none;removes that default line.height: 4px;gives the element visible thickness.background-color: #123455;fills the element with the chosen color.- The result is a solid horizontal bar that is 4 pixels tall.
Now compare it with this:
.line {
color: #123455;
}
Step by step:
- The browser applies the text color property.
- The
<hr>has no visible text to color. - The default border remains unchanged in many browsers.
Real World Use Cases
Styled <hr> elements are common in real interfaces.
Examples
- Blog posts: separating sections or article summaries
- Dashboards: dividing cards, widgets, or report sections
- Forms: visually grouping related inputs
- E-commerce pages: separating product details, reviews, and shipping info
- Landing pages: creating subtle visual breaks between content blocks
Practical styling examples
Thin neutral divider
hr {
border: none;
border-top: 1px solid #ddd;
}
Thick brand-colored divider
hr {
border: none;
height: 4px;
background-color: #123455;
}
Short centered divider
hr {
border: none;
border-top: 2px solid #123455;
width: 120px;
margin: 24px auto;
}
These patterns are useful when building polished layouts.
Real Codebase Usage
In real projects, developers rarely rely on the browser's default <hr> appearance. Instead, they usually define a reusable class or component.
Common patterns
1. Reset the default style first
hr {
border: none;
}
This avoids inconsistent rendering across browsers.
2. Create reusable utility classes
.hr-light {
border: none;
border-top: 1px solid #e5e7eb;
}
.hr-dark {
border: none;
border-top: 1px solid #374151;
}
.hr-accent {
border: none;
height: 3px;
background-color: #123455;
}
3. Use CSS variables for theming
:root {
--divider-color: #123455;
}
hr {
border: none;
border-top: 2px solid var(--divider-color);
}
This makes it easier to update colors across an entire site.
Common Mistakes
Here are common beginner mistakes when styling <hr> elements.
1. Using color instead of border or background-color
Broken example
hr {
color: red;
}
Better approach
hr {
border: none;
border-top: 1px solid red;
}
2. Forgetting to remove the default border
Some browsers apply default styles that may conflict with your custom style.
Problematic example
hr {
height: 4px;
background-color: #123455;
}
This may still show the default border.
Better approach
hr {
border: none;
height: 4px;
background-color: #123455;
}
3. Setting height without a visible fill
Comparisons
Here is a comparison of common ways to style an <hr>:
| Method | Example | Best for | Notes |
|---|---|---|---|
color | color: red; | Rarely useful for <hr> | Usually does not affect the visible line |
border | border: 1px solid red; | Simple line styling | Good when using the element's border |
border-top | border-top: 2px solid red; | Clean custom divider | Common and predictable |
background-color + |
Cheat Sheet
Quick reference
Standard thin divider
hr {
border: none;
border-top: 1px solid #123455;
}
Thick divider
hr {
border: none;
height: 4px;
background-color: #123455;
}
Short centered divider
hr {
border: none;
border-top: 2px solid #123455;
width: 100px;
margin: 20px auto;
}
Rules to remember
colorusually affects text, not the visible<hr>line<hr>is commonly styled throughborderorbackground-color- Remove browser defaults with
border: nonewhen needed
FAQ
Why does color not change the <hr> line?
Because color is mainly for text. The visible line of an <hr> is usually drawn using a border or box styling.
What is the best way to style an <hr> in CSS?
A common approach is:
hr {
border: none;
border-top: 1px solid #123455;
}
It is simple and works well for normal divider lines.
Can I make an <hr> thicker?
Yes. You can either increase the border thickness or remove the border and use height with background-color.
hr {
border: none;
height: 4px;
background-color: #123455;
}
Should I use a class instead of styling all <hr> tags?
Yes, if only some separators need custom styling. Classes make your CSS more reusable and easier to maintain.
Mini Project
Description
Build a simple article layout with multiple sections separated by custom horizontal rules. This project demonstrates how to remove the default <hr> style and replace it with reusable divider styles for different visual effects.
Goal
Create a web page that uses at least two custom <hr> styles: a thin section divider and a thicker accent divider.
Requirements
- Create an HTML page with a title and at least three content sections.
- Add an
<hr>between sections. - Style one divider using
border-top. - Style another divider using
heightandbackground-color. - Add spacing so the dividers look clean and readable.
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
CSS :not() Selector for Elements Without a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common pitfalls.
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.