CSS Layout
This topic holds the geometry questions that come up on every page: centring a box without wrapper markup, building a card grid that reflows from one column to four without media queries, keeping a sidebar beside scrollable content, pinning a header while a table body moves, and choosing between flex and grid for the same visual result. Examples list the markup they expect and the viewport range they were checked across, including narrow phone widths and 200 percent zoom. Use the task groups to separate arranging boxes from defining the tokens those boxes consume, then read each caveat for the containing-block and stacking-context rules that cause otherwise correct-looking layouts to break in one browser.
In scope: Include examples where arrangement, sizing, stacking, or positioning is the primary question. Out of scope: Exclude colour and typography work that does not change geometry.
10 published examples across 4 primary task groups. Read the group heading to find the task you are solving, then pick the Example whose assumptions match your page.
Build a responsive layout
4 examplesArrange boxes so the design holds across viewport widths, zoom levels, and content growth.
Build a card grid that reflows without media queries
Declare one minimum card width, let the grid count how many tracks fit, and a row of cards reflows from four columns to one with no media query at all.
Centre an element with flexbox and no wrapper markup
Centre one element on both axes with flexbox by giving the container a size to centre against, and keep straight which property owns which axis.
Create a two-column layout with a fixed sidebar
Split a page into a 16rem sidebar and a fluid content column with one grid track list, then collapse it to a single column before the content becomes cramped.
Keep a table header visible with sticky positioning
Pin a table header to the top of its own scroll box with position: sticky on the cells, keep borders and backgrounds honest, and avoid the wrapper that silently defeats it.
Create CSS design tokens
2 examplesExpress reusable colour, spacing, and focus values through custom properties and the cascade.
Define colour tokens for light and dark schemes
Declare intent-named colour custom properties once, re-point them under prefers-color-scheme, and keep both schemes above 4.5:1 for body and secondary text.
Style a visible keyboard focus ring with custom properties
Give keyboard focus a ring that survives both colour schemes and forced-colors mode by driving :focus-visible outlines from width, offset, and colour tokens.
Observe element visibility
1 exampleReact to intersection, viewport entry, or document visibility changes.
Lazy-load images with IntersectionObserver
Swap a placeholder image for the real file only when the browser is about to show it, and see where the loading attribute the platform already ships is the better answer.
Structure page content
3 examplesMark up document regions, reading order, and navigation so people and crawlers share one map.
Add a skip link that lands on the main region
Place a first-tab link that jumps past navigation into the main content and becomes visible on focus, so keyboard users are not trapped in the header.
Mark up a primary navigation as a labelled list
Wrap site navigation in a named nav landmark and keep its items a real list, so screen readers announce which region it is, how many links it holds, and where it ends.
Structure a page with semantic landmarks
Mark the header, navigation, main, and footer regions so keyboard and screen-reader users can jump between them instead of tabbing through everything.
Adjacent navigation
- Parent: Home
- Browse by language: JavaScript
- Browse by language: CSS
- Browse by language: HTML
- Other topics: Data Transformation
- Other topics: DOM Events
- Other topics: Fetch and HTTP
- Other topics: Form Validation
- Other topics: React State
- Or search: Search examples