CSS Flexbox Troubleshooting Checklist: Fix Wrapping, Alignment, and Overflow
When a Flexbox layout is not aligning, wrapping, sizing, or overflowing as expected, the cause is usually a small mismatch between the container, its items, and the available space. This reusable CSS Flexbox troubleshooting checklist helps you isolate the problem in DevTools, verify the relevant properties, and apply the smallest reliable fix.
Overview
Flexbox has two levels of behavior: the flex container controls the layout direction, lines, and alignment, while each flex item controls how it grows, shrinks, and sizes within that space. A rule applied to the wrong level is one of the most common reasons Flexbox appears not to work.
Start by identifying the element that owns display: flex. Then check the main axis, cross axis, available width or height, and the minimum size of each item. Remember that justify-content works along the main axis, while align-items works along the cross axis. Their visual effect changes when you change flex-direction.
For a predictable baseline, begin with a deliberately small container:
.layout {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
gap: 1rem;
justify-content: flex-start;
align-items: stretch;
}
.layout__item {
flex: 0 1 auto;
min-width: 0;
}
Once the baseline behaves correctly, add responsive rules, intrinsic content, minimum sizes, and visual refinements one at a time. This makes the change that introduced the problem easier to find.
Checklist by scenario
When items do not align
- Confirm that the parent is the flex container.
align-itemsandjustify-contentdo not affect a parent’s children unless the parent hasdisplay: flexordisplay: inline-flex. - Check the axis. With
flex-direction: row,justify-contentcontrols horizontal distribution andalign-itemscontrols vertical alignment. Withcolumn, those directions are reversed. - Inspect whether an individual item has
align-self, a fixed height, an auto margin, or an unusual margin that overrides the expected result. - For a single-line layout, distinguish between
align-itemsandalign-content. The latter distributes multiple flex lines and may appear to do nothing when wrapping is disabled.
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.75rem;
}
.toolbar__action {
margin-inline-start: auto;
}
The auto margin in this example consumes available space and pushes the action to the far edge. Remove it when testing alignment so it does not obscure what justify-content is doing.
When items refuse to wrap
- Set
flex-wrap: wrapon the container. The default is a single line, so items can shrink or overflow instead of moving to a new line. - Verify that the container has a constrained width. A flex row cannot demonstrate wrapping if its parent continues expanding to fit all content.
- Look for long, unbreakable strings, wide images, buttons with
white-space: nowrap, or fixed widths on the children. - Use
gapconsistently and account for it when estimating how many items fit on a line.
.card-list {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 16rem;
min-width: 0;
}
flex-basis gives the browser a starting size, not a guaranteed column width. The final result depends on available space, gaps, and the grow and shrink factors.
When content overflows or creates horizontal scrolling
- Inspect the overflowing item for a long word, URL, code sample, image, or replaced element with an intrinsic width.
- Try
min-width: 0on the flex item. Flex items can otherwise retain an automatic minimum size based on their content, preventing them from shrinking as expected. - For text, consider
overflow-wrap: anywherewhen breaking long tokens is acceptable. Use it deliberately because it can make identifiers harder to read. - Check descendants too. A child with
width: 100vw, a fixed width, or an unscaled image can overflow even when the direct flex item appears correctly sized.
.main {
display: flex;
gap: 1rem;
}
.main__content {
min-width: 0;
}
.main__content img {
max-width: 100%;
height: auto;
}
When equal columns have unexpected widths
Use an explicit flex shorthand when you want equal flexible columns:
.columns > * {
flex: 1 1 0;
min-width: 0;
}
The zero basis tells the browser to distribute the available space without using each item’s content size as the starting point. If one column still appears wider, inspect padding, borders, fixed widths, intrinsic content, and whether the items have different flex rules.
What to double-check in browser DevTools
- Select the actual flex container. The Layout or Flexbox panel should identify the container and its children. If no flex overlay appears, the rule may be overridden, misspelled, or applied to a different element.
- Toggle the container properties. Turn
flex-direction,flex-wrap,justify-content, andalign-itemson and off. A temporary change often reveals which axis or rule is responsible. - Check computed styles. Cross out overridden declarations and look for a more specific selector, a later rule, or an unexpected media query. Also verify that the container has usable width or height.
- Inspect each item’s box model. Compare content size, padding, border, margin, flex basis, and rendered width. Pay particular attention to automatic minimum sizing and fixed dimensions.
- Test the smallest useful case. Temporarily hide siblings, replace text with short labels, remove nested wrappers, and disable decorative styles. If the reduced example works, restore pieces until the behavior changes.
- Resize deliberately. Test just above and below the suspected breakpoint, not only at common device presets. This exposes threshold problems caused by gaps, borders, scrollbar space, or long content.
When a layout is user-facing, confirm the fix with keyboard navigation and zoom as well as viewport resizing. A visual alignment fix should not hide focus indicators, clip content, or make controls unusable. For a broader workflow, see this web accessibility testing tools guide.
Common mistakes
- Using
align-contentfor a single row. It distributes space between flex lines, so it has no meaningful effect until there are multiple lines. - Adding
height: 100%without a definite parent height. Percentage heights need a resolvable reference and can produce confusing results in nested layouts. - Assuming
flex: 1means equal visual columns in every case. Padding, borders, minimum sizes, and content constraints can still affect the rendered result. - Fixing overflow with global clipping. Applying
overflow: hiddento the page may conceal the symptom while cutting off content, focus rings, or positioned elements. Find the element that is actually too wide first. - Mixing fixed widths with shrinkable items. A fixed width, minimum width, and large gap can require more space than the container has. Decide which dimension should yield.
- Debugging only at one viewport. Flex behavior is sensitive to content and available space. A component that works with placeholder text may fail with a translated label, long username, or real API response.
Core Flexbox properties are broadly implemented in modern browsers, but newer or less common layout features should still be checked against the browser versions your project supports. Prefer a simple fallback, progressive enhancement, and feature testing over assuming that every implementation handles an edge case identically.
When to revisit this checklist
Return to this checklist whenever a responsive component breaks after a layout refactor, content change, design revision, or browser-support update. It is especially useful before a release that changes navigation, cards, tables, forms, or dashboard panels, because those components combine flexible sizing with real-world content.
Use this short final pass:
- Identify the flex container and confirm its computed display value.
- Write down the main axis, wrapping mode, and available size.
- Check item flex values, automatic minimum sizes, margins, and fixed dimensions.
- Test short, long, and empty content at narrow and wide viewports.
- Verify keyboard access, zoom, focus visibility, and absence of accidental horizontal scrolling.
- Document the final rule and the reason for any
min-width: 0, overflow, or breakpoint decision.
Keeping this reasoning close to the component makes future debugging faster. When the underlying content, design tokens, or supported browsers change, repeat the reduced test rather than adding another layout override by guesswork.