Stop measuring: size art to its box with container units
Dozens of our examples measured their box with a ResizeObserver just to hand a pixel width to an orb. A container and cqi do it in CSS, and the orb no longer jumps after the page loads.
Audrey KlammerCSSContainer queries
01orbital's orbs take a size. To make one fill its box, our examples
measured the box:
function useWidth(initial: number) {
const ref = useRef<HTMLDivElement>(null)
const [width, setWidth] = useState(initial)
useEffect(() => {
const observer = new ResizeObserver(([entry]) => {
setWidth(Math.round(entry.contentRect.width))
})
observer.observe(ref.current!)
return () => observer.disconnect()
}, [])
return { ref, width }
}
// <div ref={ref} style={{ width: "min(100%, 280px)" }}>
// <MoonSphere size={width} />
// </div>
Every example in four orb families carried a copy of that hook, and two blocks did too. Apart from the code, it had a cost you could see. The server can't measure, so the orb first drew at a guessed 240px and jumped to its real size once the page loaded.
Container units
Make the wrapper a query container and the orb can read its width in CSS.
cqi is 1% of the container's inline size; MDN lists the whole set, cqw,
cqh, cqi, cqb, cqmin and cqmax
(container query length units).
<div className="@container" style={{ width: 280, maxWidth: "100%" }}>
<MoonSphere size="100cqi" />
</div>
Tailwind's @container is container-type: inline-size. The orbs needed
one change: accept any CSS length as well as a number of px. The hook, the
ref, the state and the jump are gone.
Both axes
Our eclipse demo used to fit a disc to its frame's width and its height. The JavaScript version measured both and did the arithmetic:
Math.min(520, (width - 112) / 1.19, (height * 0.92 - 112) / 1.19)
A size container gives cqw and cqh for its width and height, so the
same arithmetic is one CSS value:
const SIZE =
"max(0px, min(520px, (100cqw - 112px) / 1.19, (92cqh - 112px) / 1.19))"
<div className="@container-size relative min-h-full w-full flex-1">
<EclipseScene size={SIZE} />
</div>
Measured in Chromium at 1280×900 and 390×700, the disc matched the old
formula to a tenth of a pixel. That demo has since changed to show the disc
at three fixed sizes, so this code is no longer in the kit; EclipseScene
still takes any CSS length as its size.
Three things to know
- A container's size can't come from its content. With
size, the element's size "can be computed in isolation, ignoring the child elements";inline-sizedoes the same for width. Give the container a width (and, forsize, a height) from layout. Our frame's height comes fromflex-1. Inside a parent that shrinks to fit its content, a container with no width of its own collapses. - No container, viewport units. With no eligible container, a
cqunit "defaults to the small viewport unit for that axis".100cqiwith no container is the viewport's width. Where a part can sit anywhere, keep accepting a number of px too. - Units are not queries. This sizes something from its box. Whether a layout should switch at a container width is a separate question. In this kit, container queries stay in parts that move between slots of different widths, and full-width sections use screen breakpoints.
The short rule
If a ResizeObserver measures a box only to size something inside it, make
the box the container and size the thing in cq units.