How each layout adapts to narrow space: which ones wrap by themselves, which follow their container, which follow the viewport and how to change the point where they switch.
Layouts respond to their own space
A component does not know where it will be used. A card grid might fill the page today and sit in a 20rem sidebar tomorrow. A viewport breakpoint cannot tell those apart, so most layouts in Vanilla Breeze do not use one. They look at the width they have been given and wrap or stack when it runs out.
The demo holds four layouts in one box. The buttons change the width of the box and nothing else. The browser window stays the same size, so no media query fires.
There are three mechanisms. The table says which layout uses which.
| Layout |
Responds to |
What happens |
| Cluster | Its own width | Items wrap to a new line |
| Grid | Its own width | Drops columns one at a time, down to one |
| Switcher | Its own width | All children in one row, or all stacked, below 30rem |
| Sidebar | Its own width | The two columns wrap when the content column would get less than half the width |
| Columns | Its own width | Fewer text columns, down to one |
| Reel | Nothing | Never wraps. It scrolls sideways at every width |
| Media | Nearest container | The figure moves above the text when the container is under 25rem |
| Prose, text, columns | Nearest container | The line-length cap is released when the container is under 30rem |
Sidebar inside a page layout's <main> | The region-main container | Each child takes a full row when main is under 40rem |
| Split | Viewport | One column below 48rem |
<main data-layout="sidebar-left"> and sidebar-right | Viewport | One column below 48rem, in the order article, aside, nav |
| Page layouts | Viewport | Sidebar templates become one column below 48rem; the dashboard nav becomes a drawer; the app shell nav moves to the bottom |
Layouts that size themselves
These use flexbox and grid rules that depend on the layout's own width. No query is involved, so they work at any nesting depth.
Grid
A grid makes as many columns as fit, each at least data-layout-min wide, then shares the leftover space between them. When the grid is narrower than one minimum, the single column shrinks to fit and does not overflow.
data-layout-min | Smallest column |
xs | 8rem |
s | 10rem |
m (default) | 15rem |
l | 20rem |
xl | 25rem |
For a width between the steps, set the --layout-min custom property on the grid.
.product-list { --layout-min: 18rem;}
Switcher
A switcher keeps its children in one row while it is wider than its threshold, and stacks them all when it is narrower. There is no in-between state with two on one line and one below.
| Attribute | Effect |
data-layout-threshold | The width where it switches: s (25rem), m (30rem, the default), l (40rem). For another width, set --layout-threshold. |
data-layout-limit | 2, 3 or 4. With more children than the limit, the switcher stacks at every width. |
data-layout-reverse | Reverses the visual order when stacked, so the last child is on top. The row keeps source order. |
The sidebar column asks for a fixed width and the content column takes the rest. When the content would get less than its minimum share of the row, the two wrap and each takes the full width.
| Attribute | Effect |
data-layout-sidebar-width | The sidebar column: narrow (12rem), normal (16rem, the default), wide (20rem) |
data-layout-content-min | The content column's smallest share before wrapping: s (40%), m (50%, the default), l (60%). For another value, set --layout-content-min. |
data-layout-nowrap | Never wrap |
With the defaults and a gap of m, the sidebar wraps when the layout is narrower than about 34rem.
Columns
With data-layout-column-count="2" or "3" the count is a maximum. A column is never narrower than 14rem, so three columns become two and then one as the element narrows. With "auto" the browser fits as many columns of the measure as it can.
Layouts that follow a container
A container query asks how wide an ancestor is. Vanilla Breeze makes every <main>, <article>, <section> and <aside> a query container (container-type: inline-size), so a query usually has something close by to measure.
The library's own container queries are few: the media object stacks under 25rem, the reading measure is released under 30rem, and a sidebar in a page layout's main stacks when main is under 40rem. The containers matter more for the CSS you write.
.order-summary { display: grid; grid-template-columns: 1fr;} @container (width >= 30rem) { .order-summary { grid-template-columns: 2fr 1fr; }}
Named regions
Inside a data-page-layout, the regions have container names, so a query can ask about the main region in particular and not whichever container is nearest.
@container region-main (width < 40rem) { .order-summary { grid-template-columns: 1fr; }}
The names are region-main, region-nav and region-aside. Grid Identity lists them with the names used inside a main-level sidebar.
Where a sectioning element is not a container
A size container has no width of its own: it takes the width its parent gives it. Where a layout sizes children by their content, a container child would collapse to nothing, so the library turns containment off for <main>, <article>, <section> and <aside> children in these places:
- Direct children of a cluster or a reel.
- Direct children of a cover with
data-layout-centered.
- Direct children of a stack with
data-layout-align set to start, center or end.
- Direct children of a grid with
data-layout-subgrid, because containment breaks subgrid.
- An article with
data-numbered or data-section-numbers, and the sections inside it, because containment restarts CSS counters.
- A floated aside (
aside.float, aside.sidenote) and any element with the popover attribute.
A query inside one of these measures the next container out. To make the element a container anyway, add data-container, and give it a width if its parent sizes it by content.
<layout-reel> <article data-container="card">...</article> <article data-container="card">...</article></layout-reel>
You can put data-container on any other element too. With no value the container has no name. The values card, panel and media name it.
Layouts that follow the viewport
Page structure is the one place where the viewport is the right thing to ask. The page templates, the main-level sidebars and data-layout="split" switch to one column below 48rem. Add data-layout-nowrap to keep the wide shape.
<section data-layout="split" data-layout-ratio="2:1" data-layout-nowrap> <p>Line 4 to Harbour</p> <p>Every 12 min</p></section>
The library uses four viewport widths. They are published as tokens for reference. A custom property cannot be used inside a media query, so write the value in your own queries.
| Token | Value | Used for |
--bp-s | 37.5rem | Compact phones. Below this a canvas surface goes flush to the edges. |
--bp-m | 48rem | The split between one column and several. Page layouts, main-level sidebars and split collapse below it. |
--bp-l | 64rem | Desktop layouts. The page table of contents becomes sticky from this width when data-sticky is on. |
--bp-xl | 87.5rem | The widest pages. The holy-grail and dashboard page layouts stop growing here. |
@media (width < 48rem) { .site-search { display: none; }}
Check the stacked order
When a layout stacks, the children appear in source order, with three exceptions:
- A switcher with
data-layout-reverse stacks last child first.
- A
<main data-layout="sidebar-left"> stacks as article, aside, nav, so a nav that comes first in the source is shown last.
- A sidebar with
data-layout-side="end" shows its <nav> or <aside> after the content at every width.
Keyboard focus and screen readers follow the source in every case. Write the source in the order the page should be read, and use these only where the visual change does not alter the meaning.