Sidebar layout with a fixed-basis sidebar and a flexible content column that wraps below it when its container is too narrow.
Overview
A sidebar layout puts a fixed-basis sidebar next to a flexible content column, and drops the content below the sidebar when there is no longer room for both. It is a wrapping flex row (display: flex; flex-wrap: wrap): the sidebar takes its basis and the content grows into the rest.
How the sidebar is chosen. The layout is semantic-aware. <nav> and <aside> children are the sidebar; every other child is content. That lets you write children in their natural DOM order and pick the visual side with data-layout-side. Injected helpers are never content: hr, [role="separator"] (the data-splitter divider), template, script and style children are skipped, so an <hr> between the columns does not become a third column.
When there is no nav or aside child, a positional fallback applies: the first child is the sidebar and the last child is the content.
When it wraps. Wrapping depends on the width of the container, not the viewport. The sidebar takes its basis (16rem by default) and the content column takes the rest; when the content would drop below its floor (data-layout-content-min, 50% by default) it wraps underneath. A sidebar inside a narrow card wraps even on a wide screen. Inside the <main> of a data-page-layout page, once that <main> is narrower than 40rem, every child of the sidebar takes a full row (in both forms) unless the sidebar has data-layout-nowrap.
Element and attribute forms
Any element can be a sidebar layout with data-layout="sidebar". The same rules and data-layout-* modifiers apply to both forms; the two below are identical, at every width.
123456789
<layout-sidebar data-layout-gap="m"> <nav aria-label="Sections">...</nav> <article>...</article></layout-sidebar> <section data-layout="sidebar" data-layout-gap="m" aria-label="Settings"> <nav aria-label="Sections">...</nav> <article>...</article></section>
Which to choose: use the attribute form when a semantic element is already the two-column region: a <section> of the page, or a <main> that holds its own navigation. Use <layout-sidebar> when there is no such element and you would otherwise add a wrapper. In the attribute form, main, article, section and aside are also container-query containers, which the element is not, so container queries inside the layout resolve against it. See data-layout for the full comparison and Semantic Layouts for the approach.
Nested sidebars start from their own defaults: an inner sidebar never inherits the outer one's gap or width settings.
Attributes
| Attribute |
Values |
Default |
Applies to |
Description |
data-layout-gap |
none, 3xs, 2xs, xs, s, m, l, xl, 2xl, 3xl |
m |
both |
Gap between the columns, on the shared size scale. |
data-layout-side |
end |
start (omit the attribute) |
both |
end: put the sidebar on the end side. There is no start value; the start side is what you get without the attribute. nav/aside children keep DOM order (order: 1); the positional fallback reverses the row (see Accessibility). |
data-layout-sidebar-width |
narrow, normal, wide |
normal |
both |
Sidebar basis: 12 / 16 / 20rem (the same three values in every sidebar context). |
data-layout-content-min |
s, m, l |
m |
both |
Content column floor before wrapping: 40 / 50 / 60%. One-off: style="--layout-content-min: 45%". |
data-layout-nowrap |
(boolean) |
— |
both |
Never wrap; the sidebar stays beside the content, also in a narrow page region. |
Custom properties
| Property | Default | Description |
--layout-content-min |
(unset) |
A content floor that beats data-layout-content-min, set inline: style="--layout-content-min: 45%". Registered with inherits: false, so a nested sidebar does not pick it up. |
Examples
Side
The sidebar sits on the start side by default (left in a left-to-right page). data-layout-side="end" moves it to the end side; there is no start value, so omit the attribute for the start side. With nav/aside children, write the content first and the sidebar second so the DOM order matches what is on screen.
1234567891011
<layout-sidebar data-layout-gap="m"> <aside aria-label="Start sidebar">Sidebar content</aside> <article>Main content</article></layout-sidebar> <layout-sidebar data-layout-gap="m" data-layout-side="end"> <article>Main content</article> <aside aria-label="End sidebar">Sidebar content</aside></layout-sidebar>
Width
The data-layout-sidebar-width attribute controls the sidebar width.
1234567891011121314151617
<layout-sidebar data-layout-sidebar-width="narrow"> <aside>Sidebar content</aside> <article>Main content</article></layout-sidebar> <layout-sidebar data-layout-sidebar-width="normal"> <aside>Sidebar content</aside> <article>Main content</article></layout-sidebar> <layout-sidebar data-layout-sidebar-width="wide"> <aside>Sidebar content</aside> <article>Main content</article></layout-sidebar>
Content minimum
data-layout-content-min sets how narrow the content column may get (40 / 50 / 60%) before it wraps below the sidebar. For a one-off value, set --layout-content-min inline, for example style="--layout-content-min: 45%"; it beats the attribute and does not reach nested sidebars.
1234
<layout-sidebar data-layout-content-min="s"> <aside>Sidebar</aside> <section>Content may shrink to 40% before wrapping</section></layout-sidebar>
No wrap
data-layout-nowrap keeps the sidebar beside the content at every width, including inside a narrow page region. Use it when the sidebar is small, such as an icon rail.
1234
<layout-sidebar data-layout-sidebar-width="narrow" data-layout-nowrap> <nav aria-label="Tools">...</nav> <section>...</section></layout-sidebar>
Sticky sidebar
Add data-layout-sticky to the nav or aside to pin it while the content scrolls. It has two prerequisites:
- The page opts into the smart sticky system with
<html data-sticky>. Without it the attribute does nothing.
- The sticky child must be a
nav or aside that is a direct child of the sidebar layout.
The sidebar is limited to the remaining viewport height and scrolls on its own when its content is taller.
123456
<html data-sticky>...<layout-sidebar> <nav aria-label="Sections" data-layout-sticky>...</nav> <main>...</main></layout-sidebar>
Resizable panels
Add data-splitter to make the sidebar resizable with a draggable, keyboard-accessible divider. The splitter switches the layout to no-wrap and lets the content column shrink freely.
123456789101112
<section data-layout="sidebar" data-splitter aria-label="Resizable file browser"> <nav aria-label="Files"> <ul class="unstyled"> <li><a href="#">index.html</a></li> <li><a href="#">styles.css</a></li> <li><a href="#">main.js</a></li> </ul> </nav> <main> <p>Drag the divider, or focus it and use the arrow keys, to resize the navigation.</p> </main></section>
Gap
The data-layout-gap attribute controls spacing between sidebar and main content.
123
<layout-sidebar data-layout-gap="xs">...</layout-sidebar><layout-sidebar data-layout-gap="m">...</layout-sidebar> <layout-sidebar data-layout-gap="l">...</layout-sidebar>
Dashboard layout
1234567891011121314151617181920
<layout-sidebar data-layout-gap="l" data-layout-sidebar-width="normal"> <aside> <nav> <ul> <li><a href="#">Overview</a></li> <li><a href="#">Analytics</a></li> <li><a href="#">Reports</a></li> <li><a href="#">Settings</a></li> </ul> </nav> </aside> <main> <h2>Dashboard Content</h2> <layout-grid data-layout-min="s" data-layout-gap="m"> <layout-card>Stat 1</layout-card> <layout-card>Stat 2</layout-card> <layout-card>Stat 3</layout-card> </layout-grid> </main></layout-sidebar>
Article with related content
12345678910111213
<layout-sidebar data-layout-gap="l" data-layout-side="end"> <article> <h2>Article Title</h2> <p>Main article content...</p> </article> <aside> <h3>Related Articles</h3> <ul> <li><a href="#">Article 1</a></li> <li><a href="#">Article 2</a></li> </ul> </aside></layout-sidebar>
Filters layout
12345678910111213141516171819
<layout-sidebar data-layout-gap="m" data-layout-sidebar-width="narrow"> <aside> <h3>Filters</h3> <select> <option>Category</option> </select> <select> <option>Price Range</option> </select> <button type="button">Apply Filters</button> </aside> <main> <layout-grid data-layout-min="s" data-layout-gap="m"> <layout-card>Product 1</layout-card> <layout-card>Product 2</layout-card> <layout-card>Product 3</layout-card> </layout-grid> </main></layout-sidebar>
Accessibility
- Keep DOM order the same as the visual order. For a sidebar on the end, write the content first and the
<aside> after it, then set data-layout-side="end". If the aside comes first in the source, the layout still moves it to the end visually, but focus and reading order then differ from what is on screen (WCAG 1.3.2, 2.4.3).
- In the positional fallback (no
nav or aside child), data-layout-side="end" reverses the whole row with flex-direction: row-reverse, so the visual order always differs from the DOM order. Prefer nav/aside children, which keep DOM order.
- Give a
nav sidebar an accessible name (aria-label or aria-labelledby) when the page has more than one navigation region.
- A sticky sidebar scrolls on its own when it is taller than the viewport. Keep focusable content (links) in it, so keyboard users can scroll it by moving focus.
- The
data-splitter divider is a focusable separator that resizes with the arrow keys.