A blog article toolbar typically holds a handful of text buttons — Edit, Share, Bookmark, Delete — that must remain readable and tappable on any screen width. Flexbox provides wrapping, spacing, and size-distribution controls that address this directly. This article builds a small demonstration toolbar and examines each property decision step by step.12
A Four-Button Toolbar That Wraps
Consider a toolbar containing four text buttons: Edit, Share, Bookmark, and Delete. The HTML is a simple container with four child elements. The CSS sets display: flex and flex-wrap: wrap on the container, and gives each button a flex-basis of 100px through the flex shorthand. In this demonstration, the container width is fixed at 320px. Because four items at 100px each require 400px of main-axis space, they exceed the container and wrap onto a second line. The first line holds three buttons, and the fourth button drops to the second line. This is the design choice for this example: the button width and container width are selected so that wrapping is visible and easy to observe. They are not measurements from a production site.12
The MDN wrapping guide explains that once there is not enough space to place another item of the specified flex-basis in a row, a new flex line is created. New lines are created as needed until all items are placed. Because the items in this example can grow (flex-grow is set to 1), they expand to fill each row completely. If only one item lands on the final line, it stretches to fill the entire line width. This stretching behavior is visible when the container is narrowed further: at around 220px, only two buttons fit per line, and if an odd number remains, the last button stretches across the full row.2
Each wrapped line acts as an independent flex container for space distribution. The MDN basic concepts guide states that any space distribution happens across each line, without reference to the previous or subsequent lines. This means a button on the second line does not align with a button above it unless their combined widths happen to produce that result. For a toolbar where visual alignment between rows matters, this single-dimensional behavior is an important constraint to understand before choosing flexbox over other layout methods.1
What Happens Without Wrapping
If you remove flex-wrap: wrap from the container, the initial value nowrap takes effect. With nowrap, items that are too wide for the container do not move to a new line. Instead, they attempt to shrink. The MDN basic concepts guide confirms that items shrink because the initial flex-shrink value is 1, which allows items to become smaller than their flex-basis. In the toolbar example, four buttons with a flex-basis of 100px inside a 320px container would each shrink proportionally so that all four remain on one line. The flex-shrink reference explains that the shrink factor is multiplied by the flex base size, distributing negative space in proportion to how much each item can shrink. This prevents smaller items from collapsing to zero before larger items are noticeably reduced.13
However, shrinking has limits. The basic concepts guide notes that using nowrap would cause an overflow if the items were not able to shrink, or could not shrink small enough to fit. An item can shrink down to its min-content size. For text buttons, the min-content size is roughly the width of the longest unbreakable word in the label. If the container is narrower than the combined min-content widths of all four buttons, overflow occurs regardless of the flex-shrink value. This condition is worth checking when you set a very small container width or use long button labels.1
Setting flex-shrink: 0 on the buttons prevents them from shrinking below their flex-basis. In that case, if the container is too narrow and flex-wrap is still nowrap, the items overflow. This is not a universal rule that flex-shrink: 0 always causes overflow — it only does so when the combined base sizes exceed the container width and wrapping is disabled. If flex-wrap: wrap is active at the same time, items with flex-shrink: 0 simply move to the next line rather than overflowing. The distinction matters for toolbar design: you can combine flex-shrink: 0 with flex-wrap: wrap to keep buttons at a fixed size while allowing them to stack vertically when space runs out.13
Preparing Your Demo Page for Public Sharing
Once your toolbar demo is working locally, you may want to share it with others or reference it in a portfolio. A publicly accessible URL requires a domain name. NamesLink offers domain registration: visit its homepage, enter your preferred name in the search box, and the system displays the registration status of that name across multiple top-level domains. You can then select an available domain and complete the purchase. This step is optional and separate from the CSS techniques above; it simply gives your demo a stable, shareable address.4
Adding Consistent Spacing with Gap
Use the gap property on the flex container to create uniform space between buttons without adding margins to individual items. The MDN gap reference explains that in flex containers with flex-direction: row, the first value defines the gap between flex lines, and the second value defines the gap between items within each line. If only one value is provided, that value applies to both dimensions. For the toolbar, writing gap: 8px on the container produces 8px of space between adjacent buttons on the same line and 8px of space between the first and second lines when wrapping occurs.52
The wrapping guide demonstrates this directly: its gutter example applies gap: 10px to a wrapping flex container and shows that the space appears between items on the same line and between lines. The guide also notes that margins, padding, justify-content, and align-content can increase the actual separation beyond what gap alone determines. For the toolbar, keeping gap as the sole spacing mechanism avoids the common problem where margin-based spacing creates uneven edges at the start and end of each wrapped line.2
Check the computed gap values in your browser's developer tools if the visual spacing seems inconsistent. The gap reference states that the generated gaps create empty spaces with the width or height of the specified size, much like an empty item or track. The visible space between elements may differ from the provided gap value because margins, padding, and distributed alignment may increase the separation. If you have also set justify-content: space-between on the container, the distributed space adds to the gap value, producing wider separation than expected. Removing justify-content and relying on gap alone gives predictable, uniform spacing for a toolbar.5
Choosing Between Growth and Fixed Size
Record whether you want toolbar buttons to expand and fill available space or remain at a fixed width. The flex-grow property controls this. Its initial value is 0, meaning items do not grow beyond their flex-basis. Setting flex-grow: 1 on all buttons distributes positive free space equally, so each button stretches to fill its line. The MDN flex-grow reference explains that if all sibling items have the same flex grow factor, all items receive the same share of remaining space. In the toolbar context, this means buttons on a line with fewer items become wider than buttons on a crowded line.6
For a toolbar where buttons should look uniform regardless of how many share a line, set flex-grow: 0 and give each button a fixed flex-basis. The wrapping guide's grid comparison section notes that when items are allowed to grow and shrink, fewer items on the last row means those items grow to fill the available space. This stretching can make a single button on the last line appear disproportionately wide. If that visual result is undesirable for your design, preventing growth keeps every button the same width and lets empty space remain at the end of each line.62
The flex shorthand combines all three values in order: flex-grow, flex-shrink, flex-basis. The basic concepts guide recommends using this shorthand to ensure all values are set explicitly. For a toolbar with fixed-width buttons that wrap, writing flex: 0 0 100px on each button means: do not grow, do not shrink, start at 100px. Combined with flex-wrap: wrap on the container, buttons maintain their size and move to the next line when space is insufficient. If you instead want buttons to shrink slightly before wrapping — perhaps to fit four on one line at a width between 320px and 400px — use flex: 0 1 100px, which allows shrinking but not growing.631
A Step-by-Step Check for Your Toolbar
The following sequence helps you verify that your toolbar behaves as intended at different widths. Each step isolates one property so you can observe its effect independently.12
If you notice that a single button on the last line stretches across the full container width, that is the expected behavior when flex-grow is positive. The wrapping guide confirms that if there is only one item on the final line it will stretch to fill the entire line. To prevent this, either set flex-grow: 0 on the buttons or accept the stretching as a design choice.2
If the spacing between wrapped lines looks different from the spacing between buttons on the same line, check whether you provided one or two values to gap. A single value like gap: 8px applies to both dimensions. Two values like gap: 12px 8px set 12px between lines and 8px between items within a line. Adjust these independently until the visual rhythm matches your design intent.5
Step 1
Set display: flex on the toolbar container and confirm that all four buttons appear in a single row when the container is wide enough. This verifies that the flex container is active and items are placed along the main axis.1
Step 2
Add flex-wrap: wrap to the container. Narrow the container width until one button drops to a second line. Confirm that the remaining buttons on the first line redistribute their space according to their flex-grow value.2
Step 3
Change flex-wrap to nowrap while keeping the narrow container width. Observe whether the buttons shrink to fit or overflow. If they overflow, their min-content size is larger than the available space divided among them.13
Step 4
Add gap: 8px to the container. Verify that space appears between buttons on the same line and between the two lines. Remove any margin rules on the buttons to avoid double spacing.5
Frequently asked questions
Why does my last toolbar button stretch to full width when it wraps alone?
When flex-grow is set to a positive value, a lone item on the final line expands to fill the entire line width. The MDN wrapping guide states that if there is only one item on the final line it will stretch to fill the entire line. To prevent this, set flex-grow: 0 on your buttons so they keep their flex-basis width regardless of available space.26
Does flex-wrap: wrap affect the gap between lines and between items differently?
The gap property handles both dimensions. In a row-direction flex container, the first value of gap sets the space between flex lines, and the second value sets the space between items within each line. If you provide only one value, it applies to both. So gap: 8px gives 8px between lines and 8px between adjacent buttons on the same line.5
Can I prevent buttons from shrinking without causing overflow?
Yes, if flex-wrap is set to wrap. Setting flex-shrink: 0 keeps buttons at their flex-basis size, and flex-wrap: wrap moves them to the next line when the container is too narrow. Overflow only occurs when flex-wrap is nowrap and items cannot shrink enough to fit.13
Should I use justify-content: space-between on a wrapping toolbar?
This is a design choice rather than a rule. The gap reference notes that justify-content can increase separation beyond the gap value. If you use both gap and space-between, the distributed space adds to the gap, which may produce uneven spacing on lines with fewer items. For predictable spacing, using gap alone is simpler. If you prefer space-between, test it at several container widths to confirm the result looks acceptable when buttons wrap.52
Basic concepts of flexbox - CSS | MDN. Retrieved 2026-10-26. ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎
Mastering wrapping of flex items - CSS | MDN. Retrieved 2026-10-26. ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎
flex-shrink CSS property - CSS | MDN. Retrieved 2026-10-26. ↩︎ ↩︎ ↩︎ ↩︎ ↩︎
域名注册入门指南 | NamesLink. Retrieved 2026-10-26. ↩︎
gap CSS property - CSS | MDN. Retrieved 2026-10-26. ↩︎ ↩︎ ↩︎ ↩︎ ↩︎ ↩︎
flex-grow CSS property - CSS | MDN. Retrieved 2026-10-26. ↩︎ ↩︎ ↩︎ ↩︎