Beyond breakpoints: applying fluid interpolation to layout

Responsive design usually arrives in discrete chunks: one layout up to a given width, another until there is room for a third, with media queries, container queries, flexbox, autoflowing grids or fluid typography handling the mechanics. Within each chunk there may be some give, but at the flip point the result is often a layout that looks slightly empty or slightly congested on either side of the switch. The alternative is to treat the whole transition as a continuum, so that the change is almost invisible and users see only the result.

A hero banner serves as the test case: an AI-generated image of a meerkat paired with text, both sized and positioned differently in the wide and stacked versions. The markup stays fixed throughout.

<div id="hero">
  <div class="details">
    <h1>LookOut</h1>
    <p>Eagle Defense System</p>
  </div>
</div>

The conventional approach styles each version separately and selects between them with a container query at a chosen width. Building the wide version attaches the background image to a ::before pseudo-element, because containers cannot query themselves, and keeps that structure so inline container query units (cqi) remain available later:

#hero {
  container-type: inline-size;
  max-width: 1200px;
  min-width: 360px;

  .details {
    position: absolute;
    z-index: 2;

    top: 220px;
    left: 565px;

    h1 { font-size: 5rem; }

    p { font-size: 2.5rem; }
  }

  &::before {
    content: '';
    position: absolute;
    z-index: 1;

    top: 0; left: 0; right: 0; bottom: 0;

    background-image: url(../meerkat.jpg);
    background-origin: content-box;
    background-repeat: no-repeat;

    background-position-x: 0;
    background-position-y: 0;
    background-size: auto 589px;
  }
}
@container (max-width: 800px) {
  #hero {
    .details {
      top: 50px;
      left: 20px;

      h1 { font-size: 3.5rem; }

      p { font-size: 2rem; }
    }

    &::before {
      background-position-x: -310px;
      background-position-y: -25px;
      background-size: auto 710px;
    }
  }
}

Converting breakpoints into expressions

Replacing the breakpoint with fluid clamp() expressions for the start and end points of the text and background lets the container query be dropped entirely.

/* Line changes
 * -50,3 +55,8
 */

/* Evaluates to -310px for a 360px wide container, and 0px for 1200px */
background-position-x: clamp(-310px, 36.905cqi - 442.857px, 0px);
/* Evaluates to -25px for a 360px wide container, and 0px for 1200px */
background-position-y: clamp(-25px, 2.976cqi);
/* Evaluates to 710px for a 360px wide container, and 589px for 1200px */
background-size: auto clamp(589px, 761.857px - 14.405cqi, 710px);

The clamp() bounds are not strictly necessary here, since the container is constrained by min-width and max-width to 360px and 1200px and the bounds match those extremes. Keeping them makes the implicit limits visible and maintainable if the constraints change later.

Each middle expression comes from solving two simultaneous equations, one for the value at each end of the range, where f is the fixed px component and v is the cqi component. For the top property:

/* Evaluates to 50px for a 360px wide container, and 220px for 1200px */
top: clamp(50px, 20.238cqi - 22.857px, 220px);
f + 12v = 220
f + 3.6v = 50
f = -22.857
v = 20.238

This resolves to 20.238cqi – 22.857px, written in a calc()-friendly form. The equations change with the unit being targeted, so the <h1> row differs:

/* Evaluates to 2rem for a 360px wide container, and 2.5rem for 1200px */
font-size: clamp(2rem, 1.786rem + 0.952cqi, 2.5rem);

There, 1cqi equals 0.75rem at a container width of 1200px and 0.225rem at 360px, with rems relative to the default UA stylesheet value of 16px:

f + 0.75v = 2.5
f + 0.225v = 2

The constants in these expressions are magic numbers by necessity — CSS cannot solve simultaneous equations. A companion calculator solves them to three decimal places and emits a comment recording their origin, which keeps the CSS readable. Similar tools exist but focus on typography and fixate on rem units; porting the JavaScript into a preprocessor is an option.

Steering around the artwork

The fluid version still has the text passing over the meerkat's head. Two adjustments fix it without any size queries.

First, the meerkat moves left faster: the background interpolation starts at 450px rather than 360px, while the other expressions stay fluid down to 360px. The start and end points of separate expressions need not align.

/* Line changes
 * -55,5 +55,5
 */

/* Evaluates to -310px for a 450px wide container, and 0px for 1200px */
background-position-x: clamp(-310px, 41.333cqi - 496px, 0px);

/* Evaluates to -25px for a 450px wide container, and 0px for 1200px */
background-position-y: clamp(-25px, 3.333cqi - 40px, 0px);

Second, the text path is bent. Two interpolations for the top coordinate run at different angles, and min() selects the smaller value. The steeper line wins at larger widths; the shallower one takes over below roughly 780px.

/* Line changes
 * -18,2 +18,9 @@
 */

/* Evaluates to 220px for a 1200px wide container, and -50px for 360px */
--top-a: calc(32.143cqi - 165.714px);

/* Evaluates to 120px for a 1200px wide container, and 50px for 360px */
--top-b: calc(20px + 8.333cqi);

/* By taking the max, --topA is used at lower widths, with --topB taking over when wider.
We only need to apply clamp when the value is actually used */
top: clamp(50px, max(var(--top-a), var(--top-b)), 220px);

For these intermediate values, iterating on the endpoints in the browser produced the result faster than choosing a formal midpoint; experimentation works as well as calculation. The interpolations can also live in custom variables as duplicates of the original, which looks cleaner than splitting the path into explicit sections with a container query — and carries the same math overhead either way.

Where pure CSS stops

These interpolations are linear, so easing and other complex motion are out of reach, and calc() output is limited to length values. There is no pure-CSS way to make opacity or a rotation angle respond fluidly to container or viewport size, and preprocessors cannot work around a browser limitation.

A few lines of JavaScript observing container width and setting a unitless custom property lift both restrictions. With that in place, the text can follow a quadratic Bezier curve.

Showing the path the heading travels as the hero banner goes from a desktop size to a tablet size to a mobile size. The path makes a smooth curve as it travels over the meerkat.

Absent JavaScript, one expression would be enough: calc(1vw / 1px) is a ratio between two lengths — at an 800px viewport, 1vw is 8px, so it should evaluate to a unitless 8, just as 1in equals 2.54cm. CSS instead fails the expression.

How far the technique scales

Fluid interpolation does not replace size queries. Some designs genuinely need to snap at fixed breakpoints, and mixing approaches is reasonable — a fluidly sized and positioned background with a query that switches grid definitions for text placement, for instance. Anchor Positioning appears promising as a way to make two elements flow around the screen together, but that remains to be explored. The meerkat example is deliberately minimal; the constraints and the arithmetic are what matter.