Build a wrapping toolbar and understand flex basis, free space, shrinking, and alignment.
Flexbox arranges items in one main direction. It is a useful starting point for a toolbar, a navigation list, or a row of cards that need to share room. In this guide you will build a toolbar with search and filter controls, then use it to learn how items grow, shrink, and wrap. The labelled controls and reset button let you test keyboard navigation without depending on a search service.
Start by identifying the flex container
Look at the HTML before writing a rule. We want to arrange three groups: a search label and input, a sort label and select, and a button. The two labels belong with their respective controls, so wrap each label and input into a single block. Make the containing toolbar a flex container. Its direct children become the flex items. The span inside a label does not become a flex item of that outer container. If you apply the layout rule to the wrong wrapper, the wrong elements can start sharing space.
This direct-child relationship is particularly important in a site built from reusable components. The markup visible in the browser may include an extra wrapper even when the source template makes several components look like siblings. Use the browser inspector to check the actual tree. If flex rules appear to have no effect, find the nearest parent and verify whether it is the flex container you meant to style.
Build a toolbar you can resize
Paste the following file into the HTML editor. It uses a consistent gap between groups, gives the search field extra room, and allows controls to wrap when they no longer fit. Test the example at a normal desktop width and at a phone width.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Flexbox toolbar practice</title>
<style>
* { box-sizing: border-box; }
body { max-width: 52rem; margin: 2rem auto; padding-inline: 1rem; font: 1rem/1.6 system-ui; color: #10244a; }
.toolbar { display: flex; flex-wrap: wrap; align-items: end; gap: 1rem; padding: 1rem; background: #e6f4ff; border-radius: .8rem; }
.toolbar label { display: grid; gap: .3rem; }
.search { flex: 1 1 15rem; min-inline-size: 0; }
.sort { flex: 0 1 11rem; min-inline-size: 0; }
.toolbar :is(input, select, button) { font: inherit; min-height: 2.75rem; padding: .4rem .65rem; }
.toolbar :is(input, select) { width: 100%; }
.toolbar button { background: #0053ad; color: white; border: 0; border-radius: .5rem; }
:focus-visible { outline: 3px solid #873f00; outline-offset: 3px; }
</style>
</head>
<body>
<main>
<h1>Community library toolbar</h1>
<p>Resize the preview to watch the controls find a new line. The example has no search backend.</p>
<form class="toolbar" id="toolbar">
<label class="search"><span>Search the catalogue</span><input type="search" placeholder="Try cycling"></label>
<label class="sort"><span>Sort results</span><select><option>By title</option><option>Newest first</option></select></label>
<button type="reset">Reset filters</button>
</form>
<script>document.querySelector("#toolbar").addEventListener("submit", event => event.preventDefault());</script>
</main>
</body>
</html>
Try it in the HTML editor
The search group is designed to grow when extra room is available. It starts from a preferred size, then it may grow or shrink according to the flex shorthand. The sort group can shrink, but it has no share of positive free space. The button keeps its natural size until it needs its own line. Give a control a sensible minimum touch height, and keep its label large enough to read at that size.
Understand where the space goes
The first part of the flex shorthand is the grow factor. It describes how available space is distributed among items that can grow. If two items have the same grow factor, they get equal shares of the available surplus. That does not automatically make their final widths equal, because they can start from different basis sizes. If you want equal widths, choose the same basis and check how the items behave when their contents are very different.
The second part is the shrink factor. It participates in how negative space is distributed when items do not fit. The basis also matters when the browser calculates shrinking. A wider starting item can surrender more room than a narrow one even if both have the same factor. The third part is the basis, or the preferred main-axis size before growth and shrinking. For the search group the basis is fifteen rem. That is a starting point rather than a promise that the group will always use that width.
An item can also have an automatic minimum influenced by the width of its content. That minimum helps preserve readability in many layouts. Here we allow the search group to shrink by setting its inline minimum to zero. The input itself uses the space of its group. If you use this technique with descriptive text, check that the words can wrap and that shrinking does not make the control unusable.
Align controls across and along the row
The main axis follows the flex direction. In the example the direction is the default row, so the groups sit next to each other in the ordinary horizontal writing mode. Justify content controls how spare space is distributed along that axis. Align items controls how the items line up across it. The example aligns items at the end of the cross axis so the bottoms of the controls line up even when some items have labels and one does not.
Change the direction to column to see the axes swap. The items now follow one another down the main axis. Cross-axis alignment affects their inline position. If the previous end alignment makes the groups look oddly narrow or offset, try stretch for a column layout. There is no universally correct alignment. Use the setting that makes the structure readable in the context of its content.
Test wrapping with real labels and long values
Make the toolbar narrow enough that it needs two lines. Wrapped flex lines distribute their items independently. If one grows on a second line, it may become wider than an item below the same horizontal position on another line. This is useful for toolbars because each line can use its own available space. If the requirement is to keep columns aligned across several rows, a grid may express that layout more directly.
Replace the two short field labels with realistic phrases from your project. Try advancing through the controls with Tab. Keep DOM order natural and avoid using visual order to conceal a poor source order. A control that looks first should not unexpectedly receive focus last when the user is navigating the form. Compare at more than one width and with browser zoom, since longer words and larger text can both cause wrapping.
If the toolbar overflows, measure the item’s width in the inspector and look for a fixed width or an intrinsic minimum. Then check which item became responsible for that space. A narrow input label may wrap gracefully while a select with a very long option may need more room. Adjust the smallest relevant wrapper instead of forcing the entire toolbar into a fixed width. When the behaviour is clear, experiment with a new layout in the CSS editor. See MDN’s Flexbox documentation for more layout patterns.
Make a deliberate choice between gap and spacing inside an item
The gap is the space between flex items. It does not pad the outer edge of the toolbar. Use the toolbar’s padding for that border space. The labels in the example also have their own small gap because their text and control are arranged by a separate grid inside the flex item. These two spacing relationships are independent. Increasing the outer gap moves the complete field groups apart. Increasing the inner gap separates a label from the control it describes.
Try setting the outer gap to two rem. At the same browser width you may see the button wrap sooner because the gaps use some of the room that was otherwise available for items. Restore the original gap and try reducing the search basis instead. This second change lets the search field start smaller, which can delay wrapping. Compare how much room a visitor has to read what they typed. A layout that squeezes every control onto a line can be worse than one that wraps cleanly.
Use the actual controls during the layout check
Enter a test phrase into the search field and select Newest first. Activate Reset filters using Tab and Enter. The text and selection should return to their starting values. Repeat the exercise after narrowing the screen. The labels should stay attached to the right fields, no controls should be clipped, and the order should remain predictable. This test catches a layout which looks tidy in a screenshot but becomes awkward when a visitor tries to operate it.