Layout patterns

Most JS layout reads could be CSS. Plus the four states every component ships without.

What flex and grid solve

When JavaScript is appropriate

Batch reads and writes. Layout reads (getBoundingClientRect) followed by writes followed by reads cause layout thrashing.

overscroll-behavior in modals

Use 100dvh instead of 100vh on mobile. 100vh includes browser chrome and overflows.

Empty, loading, and error states

Empty

Loading

Show nothing for 150-300ms. Once a spinner appears, keep it 300ms minimum to avoid flicker. Prefer skeletons for lists.

Error

State what failed, what to do, and offer an action. Preserve typed values.

Long content safety

min-width: 0 matters because flex/grid children default to min-width: auto and refuse to shrink below content width.

Common mistakes