It's common to want fluid scaling of a font size on medium screens where a page is facing reduced width but not enough to switch to the mobile layout. For example, if a page's mobile layout kicks in at 799px, items might be squeezed a bit between 800px and 999px.

The CSS below sets the font size to 22px on mobile and desktop (≤799px and ≥1000px), but smoothly decreases from 22px to 18px when the viewport is between 800px and 999px.

Caveat: We need to maintain the number used in the division within the calc(). It's max width (999) minus min width (800), or 199 in this case.

Example with px Sizing

h2.some-class {
  font-size: 22px; /* 22px at ≤799px and ≥1000px */
}

/* Fluid only between 800px and 999px */
@media (min-width: 800px) and (max-width: 999px) {
  h2.some-class {
    /* 22px at 999px → 18px at 800px (smoothly decreasing) */
    font-size: clamp(
      18px /* min size */,
      calc(18px + (4 * ((100vw - 800px) / 199))) /* fluid size */,
      22px /* max size */
    );
  }
}

Example with em Sizing (sort of)

We can't use relative units like em for the dynamic scaling because em inside calc() is relative to the current element's font size. Since we're trying to calculate the font size itself, the math collapses and it just sticks to the upper bound (1.375em).

While the calculations must use absolute units (px), we can still use relative units (em) everywhere else.

If we care that the font size transitions smoothly from the dynamic size (calculated) to the regular size (in em) as the width straddles the top of the dynamic range (999px <--> 1000px), we need to calculate the px equivalent of the dynamic size and use that in the clamp() calculation. In this case, the relative size is 1.375em which we know is 22px (assuming our relative root size is 16px).

h2.some-class {
  font-size: 1.375em; /* 1.375em (22px) at ≤799px and ≥1000px */
}

@media (min-width: 800px) and (max-width: 999px) {
  h2.some-class {
    /* 22px at 999px → 18px at 800px (smoothly decreasing) */
    font-size: clamp(
      18px /* min size */,
      calc(18px + (4 * ((100vw - 800px) / 199))) /* fluid size */,
      22px /* max size */
    );
  }
}
Published On: September 5, 2025Categories: CSSTags: , ,