How to Make a Short Underline Under a Heading in CSS

To put one short line under a heading, give ::after empty content and paint a small block. The heading text itself is unchanged, so one line sits below the last line of text even when the heading wraps.

Draw a short line below a heading

Save the code as short-underline.html and open it. Each line is 48px long when there is room. content: "" generates an empty pseudo-element, and display: block places it on its own line. Try removing content or setting it to none to make the line disappear, then restore it. CSS Pseudo-Elements · ::after

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Short decorative line below a heading</title>
  <style>
    body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
    main { max-width: 600px; }
    .section-title { margin: 32px 0 16px; color: #146c87; font-size: 24px; }
    .section-title::after {
      content: "";
      display: block;
      inline-size: 48px;
      max-inline-size: 100%;
      block-size: 3px;
      margin-block-start: 12px;
      background-color: currentColor;
    }
    .center { text-align: center; }
    .center::after { margin-inline: auto; }
    .underlined { text-decoration: underline; text-underline-offset: 4px; }
  </style>
</head>
<body>
<main>
  <h1>Short underlines and alignment</h1>
  <h2 class="section-title">Left-aligned heading</h2>
  <p>The decorative line starts at the left and is shorter than the title.</p>
  <h2 class="section-title center">A centered example that can wrap onto two lines</h2>
  <p>Use automatic margins on both sides of the block to center the short line.</p>
  <p><span class="underlined">An ordinary underline that follows the text</span></p>
</main>
</body>
</html>

Change the length and alignment

In the example, inline-size sets the length, block-size the thickness, and margin-block-start the gap. max-inline-size: 100% keeps the line inside a heading narrower than 48px. currentColor makes the line use the heading’s text color. CSS Color · currentColor

text-align: center centers the text. Center the block line separately with margin-inline: auto. To align it to the end in horizontal writing, set the heading to text-align: end and give the line margin-inline-start: auto and margin-inline-end: 0. Change only 48px to 96px to compare, then restore it.

Choose between a text underline and a decorative line

For an underline that follows the letters, use text-decoration: underline. border-bottom follows the edge of the heading box, which can be wider than its text. Use ::after when you want one line of a chosen length under the whole heading. CSS Text Decoration · text-decoration-line

Put meaningful headings and link text in the HTML and keep the pseudo-element’s content empty. Make the window narrower to see the line stay below both lines of a wrapped heading. If the heading has a fixed height, check that wrapping does not clip the decoration.