DEV Community

Daniel Pertu
Daniel Pertu

Posted on

Every size on our big screen is one number times a CSS variable, and the variable is 1cqmin

Our quiz product has two kinds of screen. There are the phones the teams hold, and there is one big screen the whole room looks at, which in practice is a laptop plugged into the pub TV or mirrored to it over AirPlay. We do not control that screen. We do not know its size, its aspect ratio, its scaling factor, or how far away the nearest table is sitting.

The same component also has to render inside a frame about 390 pixels wide, because the demo on our landing page is not a video or a screenshot. It is the real host screen, running, scaled down.

Two environments, one component, and the thing that made it work was giving up on absolute lengths almost entirely.

One unit, declared on the children

The whole stage is sized from a single custom property called --u:

export function StageFrame({ children, className = 'h-[100dvh]' }: {
    children: ReactNode
    className?: string
}) {
    return (
        <div
            className={`relative flex w-full flex-col overflow-hidden
                bg-background text-foreground
                [container-type:size]
                [&>*]:[--u:1cqmin]
                @max-3xl:[&>*]:[--u:1.8cqw] ${className}`}
        >
            {children}
        </div>
    )
}
Enter fullscreen mode Exit fullscreen mode

On a wide screen, --u is 1cqmin: one per cent of the smaller dimension of the stage container. Everything inside is then written as a multiple of it:

<div className="grid shrink-0 grid-cols-2 items-center
    gap-x-[calc(2*var(--u))] gap-y-[calc(1.6*var(--u))]
    px-[calc(3*var(--u))] pt-[calc(2.4*var(--u))] pb-[calc(1.6*var(--u))]">
Enter fullscreen mode Exit fullscreen mode

That is Tailwind arbitrary values, so it is verbose, but the property is the point rather than the syntax. A 1100 by 710 laptop stage and a 1920 by 1080 TV produce different values of --u, and every padding, radius, gap, badge and font size on the screen moves with it in exactly the same proportion. Nothing is ever almost right because one gap was in px.

Below 48rem, which is roughly a phone held upright, --u switches to 1.8cqw: a share of the width rather than the smaller dimension. On a tall narrow screen cqmin tracks the width anyway, but the multiplier bumps the whole scale up so the text is readable at arm's length instead of being sized for a room.

146 lengths and 47 multipliers

The five files that make up the big screen are 823 lines, and I counted what is actually in them:

  • 146 uses of var(--u), 145 of them a plain calc(k * var(--u)), and one where the multiplier is itself another custom property
  • 47 distinct values of k, from 0.4 to 40
  • the most common three are 2 (13 times), 1.2 (13 times) and 1.6 (12 times)

Forty-seven multipliers is more than a design system would allow and I am not going to pretend otherwise. There is no scale here, no 4-point grid, no named tokens. What there is instead is one dimension of freedom: every number on that screen is relative to the same thing, so the screen can be wrong at every size equally, which is a much easier bug to fix than being wrong at one size only.

The large end of the range is where it earns its keep. The podium area is calc(40*var(--u)) tall and the trophy inside it is calc(22*var(--u)) square. On a 13 inch laptop that is 284 and 156 CSS pixels; on a 55 inch TV at 1080p it is 432 and 237. Neither number is typed anywhere.

Why the variable is declared on the children

The detail that cost me the most time is in that [&>*] selector. The obvious thing to write is [--u:1cqmin] on the frame itself. That does not work, and the reason is in the container query spec: a container query length resolves against the nearest ancestor container, not against the element it is written on. The frame is the container. A cqmin declared there would be resolved against whatever container the frame itself is sitting inside, which on the landing page is a different box with a different size.

Declaring it on [&>*] means the value is computed one level down, where the frame is genuinely an ancestor container.

The same rule is the standing trap for anyone adding to this screen, so it is written at the top of the file: do not put anything inside the stage that is itself a container, because --u will silently start resolving against that instead.

Three places absolute lengths survive

An audit of the five files turns up exactly three kinds of length that are not multiples of --u:

  1. h-[100dvh] on the frame's default class, because the real stage fills the window and something has to say so.
  2. Icon sizes written as h-[1.1em] w-[1.1em]: ten elements, 20 declarations. These inherit from the font size next to them, which is itself a multiple of --u, so they scale, just one step removed.
  3. One single @3xl:max-w-[58cqw] on the quiz name in the lobby, which is a truncation limit rather than a size. It is a share of the container's width because what it is protecting against is a long name pushing the join instructions off the screen, and that is a width problem.

The one thing that is not proportional

Question text gets a font size chosen by how long the text is:

if (text.length > 200) return 'text-[length:calc(2.6*var(--u))] @3xl:text-[length:calc(3.2*var(--u))]'
if (text.length > 120) return 'text-[length:calc(3*var(--u))]   @3xl:text-[length:calc(3.8*var(--u))]'
return 'text-[length:calc(3.4*var(--u))] @3xl:text-[length:calc(4.4*var(--u))]'
Enter fullscreen mode Exit fullscreen mode

Three tiers, two thresholds, both picked by looking at real text rather than derived from anything. This is the one place where the screen reacts to its content rather than its size, and it stays a multiple of --u so the two systems compose: a long question on a big TV is still bigger than a short question on a laptop.

The demo scales itself with a CSS division

The landing page needs the same stage at whatever width its column happens to be. The lazy version of this is to measure the container in JavaScript and set a transform. We did not want that, because a measured scale is wrong in the server HTML and then jumps when the page hydrates.

CSS can divide two lengths, as long as you are willing to be rude about it:

<div
  className="relative w-full overflow-hidden [container-type:inline-size]
             [--canvas-w:390] [--canvas-h:760]
             md:[--canvas-w:1100] md:[--canvas-h:710]"
  style={{ aspectRatio: 'var(--canvas-w) / var(--canvas-h)' }}
>
  <div
    className="absolute top-0 left-0 origin-top-left"
    style={{
      width: 'calc(var(--canvas-w) * 1px)',
      height: 'calc(var(--canvas-h) * 1px)',
      transform: 'scale(tan(atan2(100cqw, calc(var(--canvas-w) * 1px))))',
    }}
  />
</div>
Enter fullscreen mode Exit fullscreen mode

tan(atan2(a, b)) is a / b. CSS will not divide two lengths directly, but atan2 takes two lengths and returns an angle, and tan turns that angle back into a unitless number. So the scale factor is "the width this thing actually has, divided by the width it was drawn at", computed by the browser, in the stylesheet, with no layout read.

The outer box's aspect-ratio comes from the same two numbers, so the frame reserves exactly the right height before anything renders. The inner stage is drawn at 1100 by 710 and squeezed to fit. Its own --u is then a share of the drawn size, which is fixed, which is why the scaled-down copy looks identical to the full size one instead of looking like a screen with a smaller font.

One nuance: the demo canvas is container-type: inline-size while the stage frame is container-type: size. The canvas only needs its own width for the 100cqw in that transform, and an inline-size container is cheaper, since a size container has to be layout-isolated in both axes.

See it

The host screen is the thing animating on pub-trivia.app, directly under the headline, at max-w-5xl on a laptop and max-w-[400px] on a phone. Two ways to check the claims in this post rather than take them:

  • Drag your browser window narrower and watch. Every gap, radius, badge and font size on that stage changes at the same time and in the same proportion, because they are all the same number times --u. Cross the phone threshold and the whole scale steps up at once.
  • View source on that page and search for atan2. You will find scale(tan(atan2(100cqw, calc(var(--canvas-w) * 1px)))) in the server HTML, with no JavaScript having run. Search for 1cqmin and you will find the unit declaration too.

The feature page for the same screen is pub-trivia.app/features/big-screen-display, and the leaderboard that shares its unit system is described at pub-trivia.app/features/live-leaderboard. If you want to put it on an actual TV, the free tier at pub-trivia.app/signup needs no card.

Top comments (0)