Entry 04
Sidebar that wraps on its own
Use it for any pair where one side has an ideal width and the other takes the rest, such as a media object, an input with a button, or a page with an aside.
Code
.with-sidebar {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.sidebar {
flex-basis: 20rem;
flex-grow: 1;
}
.main {
flex-basis: 0;
flex-grow: 999;
min-inline-size: 50%;
}Why it works, the rules and the credits
Why it works
- The main side's huge
flex-growtakes all the free space, which holds the sidebar at its basis. - Once the main side would get less than half the container it wraps below, and both sides grow to full width.
Rules
- The wrap point is a share of the container, not a length. Change
min-inline-sizeto move it. - Drop the sidebar's
flex-basisfor a sidebar sized by its content. - For a sticky sidebar, add
align-self: startbesideposition: stickyand its inset. The two sides stretch to the same height, and a stretched sidebar has no room to stick. - A bare
imgorvideoas one of the sides stretches to the height of the row and distorts. Setalign-items: starton the container.
Support
- Chrome
- 84
- Firefox
- 63
- Safari
- 14.1
Credits
Borrowed from: Heydon Pickering and Andy Bell, The Sidebar, Every Layout