Dotted Lines

Why CSS dashed borders fall short and how to build precise, fadeable dotted lines with repeating-linear-gradient.

border-style: dashed

Dash length & gap are browser-controlled - no CSS property changes them.

✗ rhythm fixed by browser

repeating-linear-gradient

✓ full control

border-style: dashed lets you set thickness - but dash length and gap size are decided by the browser, and there's no CSS property to override them.

border-style: dashedrepeating-linear-gradient

Tight dots

✗ browser decides
2px / 3px

Loose dots

✗ browser decides
2px / 10px

Long dashes

✗ browser decides
16px / 6px

4:1 ratio

✗ browser decides
16px / 4px

The fix: build the line from a gradient

Replace the border with a 1px-tall element whose background is a repeating-linear-gradient. The gradient tiles a segment that is exactly --dot + --gap wide: the first --dot pixels are colored, the remaining --gap pixels are transparent. Because both stops are in absolute px units, dot and gap are fully independent:

background: repeating-linear-gradient(
  to right,
  var(--color) 0, var(--color) var(--dot),
  transparent var(--dot),
  transparent calc(var(--dot) + var(--gap))
);

The same pattern works vertically - change to right to to bottom and make the element 1px wide with full height instead.

Newsletter

Stay updated with my latest articles and projects. No spam, no nonsense.