HTML Line Tag: Use and Style the hr Element with CSS

November 11, 2026 · 10 min read

HTML Line Tag: How to Use and Style the hr Element

The HTML line tag most people want is <hr>, the horizontal rule. Place it between two blocks and the browser draws a thin line across the page. It marks a thematic break, such as a change of scene or topic. You style it with CSS, which sets its color, thickness and length.

Want text to continue on a new line inside the same paragraph? That's the <br> element, a different tag with different rules. Our guide to HTML line break tags covers it.

The hr Tag: Syntax and What It Renders

<hr> is a void element. It can't hold content, and the HTML Living Standard says it takes no end tag. Both <hr> and <hr /> are valid in an HTML file, because a trailing slash on a void element has no effect. In HTML, </hr> is a parse error, and browsers ignore it. Our HTML tag list names all 13 void elements.

<p>The storm hit the harbor just after midnight.</p>
<hr>
<p>Three weeks later, the boats were back in the water.</p>

That renders as two paragraphs with a thin gray line between them, running the full width of the container. The spec's rendering section gives browsers this default style for the element.

hr {
  color: gray;
  border-style: inset;
  border-width: 1px;
  margin-block: 0.5em;
  margin-inline: auto;
  overflow: hidden;
}

Notice what's missing: a height. The element is empty, so its box is 0px tall. The line you see is really a 1px top border and a 1px bottom border pressed together, drawn in the inset style. That's all the default line tag in HTML is, and it explains most of the styling surprises below.

You can place <hr> anywhere flow content is allowed, such as inside <article>, <section>, <div> or a list item. It can't live inside a <p>, though. If you write one there, the parser closes the paragraph before the rule and your text splits in two.

When Should You Use hr? Thematic Breaks vs Decorative Lines

The spec defines <hr> as a paragraph-level thematic break: a scene change in a story, or a move to another topic within a section. It's about meaning, not looks. Browsers give it the implicit ARIA role separator, so a screen reader may announce it as a divider.

That has a practical side. A new heading already tells readers the topic changed, so an <hr> before every <h2> just adds noise. For a line that's pure decoration, such as the rule under a card title, draw a border on the element you already have.

.card-title {
  border-bottom: 1px solid #d0d7de;
  padding-bottom: 0.75rem;
}

That renders the same thin line under the title, with nothing extra for assistive tech to announce. We prefer <hr> only where the content genuinely shifts, like between scenes or between a post and its appendix, and CSS borders everywhere else. If you're stuck with a decorative <hr> in a template, role="presentation" is an allowed override.

Obsolete hr Attributes and Their CSS Replacements

Older tutorials style the rule with attributes. The WHATWG spec lists align, color, noshade, size and width on <hr> as non-conforming and says to use CSS instead. Browsers still honor them for old pages, so they work, but validators flag them. Here's what each one did and what replaces it.

AttributeWhat it didCSS replacement
align="left"Pushed a short rule to one sidemargin-left: 0; margin-right: auto;
color="red"Set the color and made the line solidborder-color or background-color
noshadeDrew a flat line instead of the inset oneborder-style: solid;
size="4"Set the thickness in pixelsheight or border-top-width
width="50%"Set the length in pixels or percentwidth: 50%;

The mapping isn't a guess. The spec's rendering section turns each attribute into CSS, such as margins for align, height for size and width for width, which is how browsers still draw old pages.

How Do You Change the Color and Thickness of an hr Line?

Most failed attempts come from that 0px box with borders on every side. Set only height: 5px and you get a hollow gray rectangle, because all four borders are still there. Set only background-color and you'll barely see a change, since there's no content area to paint. Set only color and the line does change color, but the inset style can still shade the top and bottom edges differently.

The fix is to reset the default border first. Two patterns work in every current browser.

hr.rule-border {
  border: none;
  border-top: 3px solid #1f6feb;
}

hr.rule-fill {
  border: 0;
  height: 3px;
  background-color: #1f6feb;
}
<hr class="rule-border">
<hr class="rule-fill">

Both render a flat blue line 3px thick. In the first, border-top sets the thickness and color. In the second, height sets the thickness and the background provides the color. Pick the border version for dashed or dotted lines, and the fill version when you want rounded ends with border-radius or a gradient.

If your site has a dark theme, set color: inherit on the rule and use currentColor as the line color, as in border-top: 1px solid currentColor. The rule then follows the text color in both themes. Skip the inherit and it stays gray, because the default stylesheet sets the rule's own color to gray. Add opacity: 0.3 to soften it, rather than hard-coding a separate gray for each theme.

Styling hr with CSS: Width, Centering, Dashed and Gradient Lines

Width is the easy part. The default margin-inline: auto already centers the rule, so a narrower one stays centered on its own.

hr.short {
  width: 40%;
}

That renders a line 40% as wide as its container, centered. To pin it to the left instead, add margin-left: 0. Watch for CSS resets, though, since some replace the auto margins and the centering disappears.

Those auto margins also explain a rule that vanishes inside a flex or grid container. There, auto side margins shrink the element to its content width, and an empty <hr> has none, so you get a dot or nothing. Give the rule an explicit width: 100% and the line comes back.

For a different line style, change the border style on the top edge.

hr.dashed {
  border: none;
  border-top: 2px dashed #8c959f;
}

hr.double {
  border: none;
  border-top: 4px double #8c959f;
}

The first renders a dashed gray line. The second renders two thin parallel lines. It needs a border at least 3px thick, because double splits that thickness between two strokes and a gap.

A line that fades out at both ends uses the fill pattern with a gradient.

hr.fade {
  border: 0;
  height: 1px;
  background-image: linear-gradient(to right, transparent, #57606a, transparent);
}

That renders a 1px line that's darkest in the middle and disappears toward the edges.

How Do You Make a Vertical Line in HTML?

There's no vertical line element, and <hr> is horizontal by definition. For decoration, draw a border-left on an existing element, the way the card example above uses border-bottom. When the divider separates groups of controls, say in a toolbar, give it the separator role and a vertical orientation.

<div class="toolbar">
  <button>Cut</button>
  <button>Copy</button>
  <div class="v-rule" role="separator" aria-orientation="vertical"></div>
  <button>Undo</button>
</div>
.toolbar {
  display: flex;
  gap: 8px;
}

.v-rule {
  border-left: 1px solid #8c959f;
  align-self: stretch;
}

That renders a 1px gray line between Copy and Undo, as tall as the buttons. align-self: stretch makes the divider fill the flex row's height, and aria-orientation tells assistive tech it runs vertically. The separator role defaults to horizontal without it.

Using hr Inside a select Dropdown

The spec now lets <hr> sit inside a <select> as a separator between options. According to MDN's compatibility data, desktop Chrome added it in version 119, Firefox in 122 and Safari in 17.

<label for="size">Size</label>
<select id="size" name="size">
  <option>Small</option>
  <option>Medium</option>
  <option>Large</option>
  <hr>
  <option>Custom size</option>
</select>

When the menu is open, a line appears between Large and Custom size. There's a real limitation here. MDN notes that none of those browsers expose the rule to the accessibility tree, and Chrome shows it only in the expanded menu. Older browsers drop it, and the list still works without the line. Treat it as visual polish, and use <optgroup> when the grouping actually matters.

The SVG line Element: The Other HTML Line Tag

Search for an HTML tag for a line and you'll also find <line>. That's an SVG element, and it only works inside an <svg>. You give it two points, and it draws a straight line between them at any angle.

<svg width="200" height="20" aria-hidden="true">
  <line x1="0" y1="10" x2="200" y2="10" stroke="#1f6feb" stroke-width="2" />
</svg>

That renders a blue line 200px long and 2px thick. Leave out stroke and nothing appears, because the SVG line has no stroke by default. Use it for diagonals, charts and diagrams. For a break between sections of text, <hr> is still the right element.

How hr Shows Up in Markdown

Markdown has its own divider syntax, and Markdown renderers turn it into an <hr> element. The Markdown horizontal line guide covers the syntax variants and their pitfalls. In our editor, the toolbar's Horizontal Rule button inserts one for you. Because the output is ordinary HTML, the CSS patterns above apply unchanged. That's one place where Markdown and HTML meet directly.

To see it, press Export HTML below. The preview pane switches to the HTML output, where you'll find the hr element between the two scenes.

The storm hit the harbor just after midnight. By morning, half the fleet was gone.


Three weeks later, the boats were back in the water, and nobody talked about that night.

32 words177 characters5 lines
Markdown

For a full stylesheet that covers rules, headings and tables together, see the Markdown CSS themes guide. If you're cleaning up hand-written markup with a few rules in it, the HTML formatter beautifies or minifies it in your browser and runs a basic issue check.

The HTML line tag you'll reach for most is <hr>, and it works best when you treat it as meaning rather than decoration. Reset its default border, set the color and thickness with one of the two CSS patterns, and keep borders for purely visual lines. To test a divider in Markdown, open the editor, click Horizontal Rule, and watch the line appear in the live preview.