日本語

How to Draw Lines on Both Sides of a Heading with CSS

​​CSS​CSS Grid,Flexbox,HTML,Playwright

To draw lines on both sides of a heading, set the heading to display: flex and use its ::before and ::after pseudo-elements as the lines. Giving both pseudo-elements flex: 1 splits the space left over after the text width between the two sides, so you do not need to adjust the line length when the number of characters in the heading changes. The HTML only needs the heading text.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened the HTML and CSS page in Playwright and recorded the computed widths of the pseudo-elements (getComputedStyle()), the text positions, the light blue pixels in screenshots, and the accessibility tree. I did not test Firefox, Safari, or real smartphones. The browser versions listed for each feature are the values from the compatibility data (@mdn/browser-compat-data 8.1.8).

A screen showing five headings reading "neko no te de" stacked vertically. The first has light blue lines on both sides of the text, extending to the edges of the screen. The second has short lines on both sides of the text, with the whole group centered. The third has the text at the left edge, with a line extending only on the right. The fourth has dotted lines on both sides, and the fifth has double lines on both sides. (The text in the image is Japanese.)
Headings with lines drawn using the CSS in this article. From top: the basic form, shortened lines, right side only, dotted, and double

Make the heading flex and turn ::before and ::after into lines

The HTML only needs a class on the heading.

html
<h2 class="heading-lines">猫の手で</h2>

The CSS is as follows.

css
.heading-lines {
  display: flex;
  align-items: center;
  gap: 0.75em;
}

.heading-lines::before,
.heading-lines::after {
  content: "";
  flex: 1;
  border-top: 2px solid #00c8e8;
}

::before creates a pseudo-element as the first child of the element. ::after creates one as the last child. Neither is displayed without content, so the code uses an empty content: "".

When the heading is display: flex, the ::before, the heading text, and the ::after line up horizontally. A single number given to flex has the same meaning as flex: 1 1 0%. It sets the starting width to 0 and shares the free space. Since both pseudo-elements have flex: 1, each takes half of the space left after the text width.

The pseudo-elements have no content, so the only visible part is the 2px border-top. This becomes the horizontal line beside the heading. align-items: center places the line at the vertical middle of the heading. gap is the space between the text and each line.

In the test environment, for a 568px-wide heading (font size 24px), each side line measured 218px, the text measured 96px, and the space between the text and each line was 18px (0.75em). The text sits exactly at the center of the heading. In the screenshot, light blue lines were drawn on both sides in the row 17 to 18px below the top of the heading. The heading height was 36px, which also matches the vertical middle of the text.

Shorten the lines and center the group

To set the line length explicitly, write the pseudo-element's flex as something like 0 0 3em. It neither grows nor shrinks, so the line is always 3em long.

The classes introduced below (such as .heading-short) are used together with the initial .heading-lines class.

html
<h2 class="heading-lines heading-short">猫の手で</h2>
css
.heading-short {
  justify-content: center;
}

.heading-short::before,
.heading-short::after {
  flex: 0 0 3em;
}

Once the lines stop growing, there is empty space on the right. If you add nothing, the lines and text move to the left. In the test environment, when justify-content: center was removed, the text started 90px from the left edge of the heading. Adding justify-content: center grouped the three items in the center, and the text returned to the middle. Both lines were 72px (3em of a 24px font).

Draw a line on one side only

To keep the text on the left and extend a line only on the right, remove ::before.

css
.heading-right::before {
  content: none;
}

Setting content to none prevents ::before from being generated. Only ::after remains, and it stretches with flex: 1, so the text stays at the left edge and the line extends to the right edge. In the test environment, the line was 454px long, which is the 568px heading width minus the 96px text and the 18px gap. To draw the line on the left instead, remove ::after.

Use dotted or double lines

To change the line style, change the style in border-top. Any value accepted by border-top-style can be used.

css
.heading-dashed::before,
.heading-dashed::after {
  border-top: 2px dashed #00c8e8;
}

.heading-double::before,
.heading-double::after {
  border-top: 4px double #00c8e8;
}

For the double line (double), I used a width of 4px. In the screenshot, two light blue lines were drawn on each side, in the rows 16px and 19px below the top of the heading. With the dashed style, short light blue segments were lined up at regular intervals along the positions of the left and right lines.

When a long heading wraps, the lines disappear

With the first CSS as it is, the lines disappear when the heading is long.

Two headings stacked vertically. In the upper one, labeled "flex: 1 (no minimum line width), the long heading wraps to two lines and is left-aligned, and there are no lines anywhere. In the lower one, labeled "flex: 1 0 2em with text-align: center", the heading wraps to three lines, is centered, and has short light blue lines at the vertical middle on both sides. (The labels in the image are Japanese.)
A long heading shown at a screen width of 400px. The top uses the first CSS, and the bottom gives the lines a minimum length

In the test environment, when a long string was put in the 568px-wide heading, the text wrapped to two lines and the pseudo-elements on both sides measured 0px. The screenshot also had no light blue lines. flex: 1 starts from a width of 0 and only takes the free space. When the text fills the full width of the heading, there is no free space, so the line length becomes 0. The gap space remains, so the text stayed left-aligned and wrapped, starting 18px from the left edge.

To keep the lines, give the pseudo-elements a minimum length. At the same time, center the wrapped text.

css
.heading-lines {
  display: flex;
  align-items: center;
  gap: 0.75em;
  text-align: center;
}

.heading-lines::before,
.heading-lines::after {
  content: "";
  flex: 1 0 2em;
  border-top: 2px solid #00c8e8;
}

flex: 1 0 2em means a starting width of 2em, growing to use free space, and not shrinking (flex-shrink: 0). When I tried it with the short heading "neko no te de", the lines grew to 218px, the same as with the first CSS. With the same long heading in the test environment, the lines stayed at 48px (2em) on both sides, and the text wrapped to two lines and was centered. Even when the screen width was 360px (heading width 328px), the lines remained 48px and the text wrapped to four lines. In both widths, the line height matched the vertical middle of the whole wrapped text.

text-align: center is set on the heading, and it also applied to the wrapped text. As described in the CSS Flexbox specification, flex items section, inside a flex container the heading text is wrapped in an anonymous box with no element, and that becomes one item. The CSS Display specification states that anonymous boxes inherit inheritable properties from their parent box (and text-align is one of them). Because this box inherits the heading's text-align, the lines inside it are centered.

If the heading contains tags, wrap the text in a span

If part of the heading text is wrapped in <em> or <a>, gaps appear between the characters.

html
<h2 class="heading-lines">猫の<em>手</em>で</h2>
Two headings stacked vertically. In the upper one, labeled "em inside the heading", there are wide gaps between "neko no", "te", and "de". In the lower one, labeled "wrapped in one span", "neko no te de" is displayed tightly with lines extending on both sides. (The labels in the image are Japanese.)
A heading with an em inside, and the same heading with all the text wrapped in a span

As noted above, inside a flex container each child element, and each run of text separated by elements, becomes its own item. In this HTML, the text splits into three items: "neko no", "<em>te", and "de". The 18px gap is inserted between each of them. In the test environment, "neko no" was at 234 to 282px, "te" at 300 to 324px, and "de" at 342 to 366px, each 18px apart.

If you wrap the whole heading text in one <span>, it becomes a single item.

html
<h2 class="heading-lines"><span>猫の<em>手</em>で</span></h2>

Doing this kept the characters together, and the side lines measured 218px, the same as for the heading without tags.

Writing the same look with grid

The same appearance can be built with display: grid. Divide the columns into three parts, "line, text, line", and give the two outer columns a minimum length with minmax().

css
.heading-grid {
  display: grid;
  grid-template-columns: minmax(2em, 1fr) auto minmax(2em, 1fr);
  align-items: center;
  gap: 0.75em;
  text-align: center;
}

.heading-grid::before,
.heading-grid::after {
  content: "";
  border-top: 2px solid #00c8e8;
}

minmax(2em, 1fr) is a column with a minimum of 2em that shares the remaining space when there is room. In the test environment, the short heading had 218px lines and the long heading had 48px lines, the same results as flex: 1 0 2em with flex. However, the grid also splits the text at tags, and the breakage is larger. When the heading 猫の<em>手</em>で was used with grid, the items became five: "line, neko no, te, de, line". They did not fit in three columns and moved to a second row. In the test environment, "de" and the right line moved to the second row, and the heading height grew from 36px to 90px. When using grid, wrap the whole text in a <span> as well.

The lines are not read aloud or included in text selection

Because the content: "" pseudo-elements contain no text, the heading content does not change. In the test environment's accessibility tree, the heading appeared as heading "猫の手で" [level=2], and nothing corresponding to the lines appeared. Both innerText and the text selected when selecting the heading's contents (Selection.toString()) were just 猫の手で.

Browser support, according to the compatibility data, is as follows. The flex gap is the newest: Chrome and Edge from 84, Firefox from 63, and Safari from 14.1 (iOS Safari from 14.5). flex is available from Chrome 29, Firefox 22, and Safari 9, and minmax() from Chrome 57, Firefox 52, and Safari 10.1.

​Posted by 86