brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: CSS text-underline-offset moves an underline away from the text

Css
~/tips/tip-css-text-underline-offset

a {

Controls the distance between text and its underline, so links stop cutting through descenders.

Default underlines hug the text and slice through the tails of letters like g and y. A little distance makes links much easier to read, and CSS has a property for it.

a {
  text-decoration: underline;
  text-underline-offset: 0.2em;
}

The property sets how far the underline sits from its default position. The initial value is auto, where the browser decides. A length overrides both the browser default and any offset suggested by the font file. MDN recommends em, so the offset grows and shrinks with the font size.

Combine it with text-decoration-thickness and text-decoration-color to style links without borders or background tricks:

a {
  text-decoration-color: currentColor;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.25em;
}

A percentage is a percentage of 1em in the element’s own font. It is inherited as a relative value and stays constant across the whole box, even when child elements have different font sizes.

MDN marks the property as widely available, with support across browsers since November 2020.

The caveat is the scope. It affects only underlines, not overlines or strikethroughs, and it is not part of the text-decoration shorthand, so setting that shorthand later does not reset your offset. If you need the offset on specific links only, set it on those selectors.

Further reading: MDN: text-underline-offset

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link