HTML
CSS
Web Design
Web Development
Front-End Development

Changing the color of an hr element

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

The <hr> tag in HTML is used for creating a horizontal rule, or line, which serves as a thematic break between paragraphs of text. By default, the appearance of an <hr> tag is fairly straightforward — typically a horizontal line across the container. However, modern CSS provides several properties to enhance and modify the visual presentation of an <hr> element, including its color.

Changing the Color Using CSS

Changing the color of an <hr> element with CSS can be done in a few ways, depending mainly on your design requirements and the specific styling approach you are adopting. Below are several methods to achieve this.

Using the color Property

The simplest way to change the color of an <hr> is to use the color property. This method generally affects the border color, since an <hr> is traditionally represented by its border.

css
hr {
    color: blue;  /* This sets the border color of the hr */
}

Using the border-color Property

For more specific control over the borders, you might prefer using the border-color property. This is particularly useful when you have customized the border styles.

css
1hr {
2    border-top: 2px solid;
3    border-color: red;
4}

Using Background Color with background-color

When you style an <hr> to have a solid look by removing the border and using background color, here’s how you would change its color:

css
1hr {
2    border: none;      /* Remove default border */
3    height: 5px;       /* Give it thickness */
4    background-color: green; /* Apply background color */
5}

Advanced Styling

Creating Gradient Color

With trends leaning towards more visually appealing design elements, you might want to implement a gradient color on your <hr>.

css
1hr {
2    height: 5px;
3    border: none;
4    background: linear-gradient(to right, red, yellow);
5}

Adding Transparency

For a more subtle design, you might add transparency to the color of your <hr> using RGBA color values.

css
1hr {
2    border: none;
3    height: 3px;
4    background: rgba(255,0,0,0.5); /* Red with 50% opacity */
5}

Usage Considerations

When changing the color of an <hr> tag, consider the overall design of your website or application to ensure it complements other elements. Consistency is key in design.

Summary Table

PropertyPurposeExample Value
colorSets border colorcolor: blue;
border-colorSpecifically alters border colorborder-color: red;
background-colorChanges the element's colorbackground-color: green;
background (for gradients)Applies a gradient backgroundlinear-gradient(to right, red, yellow);
rgba() (for transparency)Adds color with transparencyrgba(255,0,0,0.5);

This guide highlights basic and advanced methods for changing the look of an <hr> element, demonstrating how easily you can customize this standard HTML feature to fit your design needs.


Course illustration
Course illustration

All Rights Reserved.