No max-font-size in CSS: how to cap fluid type

CSS ships without a max-font-size property, so capping type that scales with the viewport means reaching for a workaround. The need arises because font-size accepts dynamic values such as font-size: 10vw;. Viewport units make the text grow and shrink along with the browser window, and a max-font-size (or a matching min-font-size at the other end) would put a limit on how far that scaling goes.

The simplest route is a media query at a chosen screen-size breakpoint that switches the font size over to a non-relative unit.

body {
  font-size: 3vw;
}
@media screen and (min-width: 1600px) {
  body {
     font-size: 30px;
  }
}

A fancier approach is known as CSS locks, which scales a value gradually between a minimum and a maximum.

body {
  font-size: 16px;
}
@media screen and (min-width: 320px) {
  body {
    font-size: calc(16px + 6 * ((100vw - 320px) / 680));
  }
}
@media screen and (min-width: 1000px) {
  body {
    font-size: 22px;
  }
}

The technique has become, or will become, a lot simpler. CSS includes a max() function, which reduces the example above to a single line:

font-size: max(30vw, 30px);

Combining a minimum and a maximum looks like this:

font-size: min(max(16px, 4vw), 22px);

That is equivalent to:

font-size: clamp(16px, 4vw, 22px);

Browser support for these functions was rather sparse at the time of writing, though Chrome already had them. Support will arrive eventually, but anyone who needs the behavior now should fall back to the first option in this article.

With these functions available, it seems unlikely that CSS will ever gain min-font-size and max-font-size, since the functions read almost more clearly as they are.