How to style an hr element with CSS?


To separate the content on an HTML web page, the <hr> element is used. The <hr> tag only consists of the opening tag i.e.; no closing tag is required. Insert a horizontal rule i.e., a line using the <hr> element. To style the <hr> element, the border properties play a major role. The border shorthand property or if you want to style a specific area of the horizontal line, then border-top, border-bottom, border-left, etc. properties can be used. Let us see how to style the <hr> element with HTML and CSS.

Set the hr element

For our example, we have set the <hr> element here. Five <hr> elements are set to style them separately with classes −

<hr class="solid">
<hr class="dashed">
<hr class="double">
<hr class="dotted">
<hr class="round">

Solid horizontal line

Here, we will create a solid horizontal line and style it with the border-top property −

.solid {
   border-top: 5px solid rgb(16, 2, 141);
}

Dashed horizontal line

Here, we will create a dashed horizontal line and style it with the border-top property −

.dashed {
   border-top: 5px dashed rgb(190, 13, 146);
}

Double horizontal line

Here, we will create a double horizontal line and style it with the border-top property −

.double {
   border-top: 5px double rgb(22, 145, 11);
}

Dotted horizontal line

Here, we will create a dotted horizontal line and style it with the border property −

.dotted {
   border: 5px dotted rgb(200, 255, 0);
}

Round horizontal line

Here, we will create a dotted horizontal line and style it with the border-radius property −

.round {
   border: 5px solid rgb(35, 196, 142);
   border-radius: 5px;
}

Example

To style an hr element with CSS, the code is as follows −

<!DOCTYPE html>
<html>
<head>
   <style>
      body{
         font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
         margin: 20px;
      }
      hr{
         margin-bottom: 20px;
      }
      .solid {
         border-top: 5px solid rgb(16, 2, 141);
      }
      .dashed {
         border-top: 5px dashed rgb(190, 13, 146);
      }
      .double {
         border-top: 5px double rgb(22, 145, 11);
      }
      .dotted {
         border: 5px dotted rgb(200, 255, 0);
      }
      .round {
         border: 5px solid rgb(35, 196, 142);
         border-radius: 5px;
      }
   </style>
</head>
<body>
   <h1 style="text-align: center;">HR styling example</h1>
   <hr class="solid">
   <hr class="dashed">
   <hr class="double">
   <hr class="dotted">
   <hr class="round">
</body>
</html>

Updated on: 21-Dec-2023

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements