日本語

CSS Vertical Timeline with Dots and a Line

​​CSS​HTML,Playwright

To build a vertical timeline of events with dots and a line, put a dot on each <li> of an <ol> with ::before, and draw the line with ::after. Make each line run from the center of its item's dot to the center of the next item's dot, and skip the line on the last item. Stack the dots above the line with z-index. If you calculate the dot's top offset in lh units from the height of the first line, the dots stay aligned with the date text even when the font size changes.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened a page with only HTML and CSS in Playwright at a 360px-wide viewport, then read the pixels in the screenshots to measure the positions of the line and dots. I did not test Firefox, Safari, real smartphones, or screen reader announcements.

Vertical timeline. A light blue vertical line runs along the left side. Next to the dates Nov 1 to Nov 4 (labels in Japanese), there is one light blue ring with a white center for each date. The line connects the first dot to the last dot and does not extend below the last dot.
The timeline made with the CSS in this article (360px wide)

Write the HTML with ol and li

The events are in time order, so use an <ol>, which represents an ordered list. Wrap each date in a <time> element and write a machine-readable date in the datetime attribute.

html
<ol class="timeline" role="list">
  <li>
    <time datetime="2026-11-01">11月1日</time>
    <p>下書きを書き始めました。</p>
  </li>
  <li>
    <time datetime="2026-11-02">11月2日</time>
    <p>画像を3枚作りました。</p>
  </li>
  <!-- … -->
</ol>

I add role="list" because if you remove the numbering with list-style: none in CSS, Safari stops treating the list as a list. According to MDN's page on list-style, Safari does not treat a list with list-style: none as a list in the accessibility tree, except when it is inside a <nav>. Adding role="list" restores the list semantics without changing the appearance.

CSS that draws the dots and the line with pseudo-elements

css
.timeline {
  --dot: 14px;  /* dot diameter */
  --line: 2px;  /* line thickness */
  --x: 8px;     /* position of the line's center from the left edge */
  list-style: none;
  margin: 0;
  padding: 0;
}
.timeline li {
  position: relative;
  padding: 0 0 24px 32px;
  line-height: 1.6;
}
.timeline time { display: block; font-weight: bold; color: #067a98; }
.timeline p { margin: 4px 0 0; font-size: 14px; }

/* Dot: place it so its center is on the line's center and at the vertical middle of the first line */
.timeline li::before {
  content: "";
  position: absolute;
  left: calc(var(--x) - var(--dot) / 2);
  top: calc(0.5lh - var(--dot) / 2);
  width: var(--dot);
  height: var(--dot);
  box-sizing: border-box;
  border: 3px solid #00c8e8;
  border-radius: 50%;
  background: #fff;
  z-index: 1; /* draw above the line */
}

/* Line: from the center of this item's dot to the center of the next item's dot */
.timeline li:not(:last-child)::after {
  content: "";
  position: absolute;
  left: calc(var(--x) - var(--line) / 2);
  top: 0.5lh;
  bottom: -0.5lh;
  width: var(--line);
  background: #00c8e8;
}

I give each li position: relative and place the dot and the line with position: absolute in the left margin of the li. As MDN's page on position explains, an absolutely positioned element is placed relative to its nearest positioned ancestor, and top and left are distances from the edges of that containing block. Here, the nearest positioned ancestor is the li with position: relative, so the li is the containing block. ::before and ::after are not rendered without content, so I set it to an empty string.

To align the centers of the line and the dot, I set the left position to the line's center position --x minus half the element's width. In the test environment, the computed left of the dot was 1px (the diameter is 14px, so the center is at 8px), and the computed left of the line was 7px (the thickness is 2px, so the center is at 8px). Both centers were at the same position.

Do not extend the line below the last dot

You can also draw one line for the whole list by giving the ol position: relative and extending ol::before from top: 0 to bottom: 0. However, the length of this line depends on the height of the list, so the line continues below the body text of the last item.

Comparison of two versions, one above the other. In the top version, "a single line across the whole list," the vertical line continues below the dot for Nov 4 (label in Japanese). A pink frame highlights this part. In the bottom version, "a line per item up to the next dot," the vertical line ends at the dot for Nov 4.
A line across the whole list extends below the last dot (top). Drawing a line per item up to the next dot ends it at the last dot (bottom).

At 360px wide, the single line across the whole list continued 78px below the bottom edge of the last dot, and it extended about 6px above the first dot. With the CSS in this article, the line started at the first dot and ended at the last dot. I found no row between dots where the line color was interrupted.

When drawing a line per item, set the top of the line to the center of the item's own dot (top: 0.5lh), and its bottom to 0.5lh below the bottom edge of the li (bottom: -0.5lh). The li has no vertical margin, so the next li starts right at the bottom edge of this one. Then 0.5lh below that point is the center of the next item's dot. In the test environment, the line was not interrupted between dots at either 16px or 22px font size, and it ended at the last dot. To identify the last item, I use :last-child, and :not(:last-child) prevents the line from being drawn on it.

If the line shows through the dots, add z-index to the dots

If you make the dots hollow rings without z-index, the line shows in the lower half of each dot. As MDN's page on ::before explains, when z-index is not set, ::after is painted after ::before and therefore appears on top. The line (::after) starts at the center of the dot, so it overlaps the lower half of the dot (::before) of the same li.

Comparison of two versions, one above the other, with the first dot enlarged 4 times. In the top version, "no z-index on the dot," a vertical line enters the hollow ring from the center downward. A pink frame highlights this. In the bottom version, "z-index: 1 on the dot," the inside of the ring is white, and the line starts outside the bottom of the ring.
Without z-index, the line overlaps the lower half of the dot (top). With z-index: 1, the dot is painted above the line (bottom).

I checked the color of the pixel 2px below the center of each dot. Without z-index, it was light blue for the first three dots, which have a line, and white for the last dot, which does not. With z-index: 1 on the dots, all four were white.

Set the position with lh so dots stay aligned when the font size changes

If you set the top of the dot to a fixed value such as top: 6px, it lines up with the date when the font size is 16px, but it drifts when you enlarge the font. Instead, I use the lh unit, which represents the height of one line, so the center of the dot is at the vertical middle of the first line. MDN's page on CSS length units explains that lh is equal to the computed value of the element's line-height.

Comparison of two versions, one above the other, with the first item enlarged. The font size is 22px. In the top version, "top: 6px," the dot sits higher than the text of Nov 1 (label in Japanese). A pink frame highlights this. In the bottom version, "top: calc(0.5lh - 7px)," the dot is centered on the height of the text for Nov 1.
With a 22px font size. If top is set in px, the dot shifts upward (top). If it is calculated from lh, the dot aligns with the date (bottom).

I set the font size of the li to 22px and measured the difference between the center of the dot and the center of the date line.

Dot top At 16px At 22px
6px 0.2px 4.2 to 4.8px too high
calc(0.5lh - 7px) 0.2px within 0.4px

At 16px, the computed line-height of the li was 25.6px, and the computed top of the dot was 5.8px (half of 25.6px minus 7px). At 22px, line-height was 35.2px and the dot's top was 10.6px, so the dot moved down along with the text. This shows that lh in a pseudo-element is calculated from the height of one line of the li. The line also uses 0.5lh for its top and bottom, so it connects the centers of the dots.

​Posted by 86