Skip to main content
Beta: Front-End Checklist is currently in beta. Some issues are still being fixed. Thanks for your patience.
CSSMedium

Apply Flexbox best practices

Use Flexbox for one-dimensional layouts with the right properties, avoiding common mistakes like overusing flex:1, ignoring min-width:0, and misunderstanding flex-basis.

Utilities
Quick take
Typical fix time 20 min
  • Flexbox is for one-dimensional layouts (a row OR a column)
  • Use flex: 1 1 0 (shorthand: flex:1) for equal-width items that fill space
  • Add min-width: 0 to flex children when text overflow truncation doesn't work
  • Use gap instead of margin for spacing between flex items
Why it matters: Flexbox is one of the most used CSS features, but its unintuitive default values (flex-shrink:1, min-width:auto) cause layout bugs that are hard to diagnose. Understanding the flex shorthand and the min-width:0 fix prevents the most common Flexbox breakage scenarios.

Rule Details

Flexbox is purpose-built for one-dimensional layouts. Understanding its three core properties (flex-grow, flex-shrink, flex-basis) prevents the most common bugs.

Code Example

/* flex: grow shrink basis */
flex: 0 0 auto;    /* Don't grow, don't shrink, use content size (default) */
flex: 1 1 0;       /* Grow and shrink equally, start from 0 (shorthand: flex:1) */
flex: 1 1 auto;    /* Grow and shrink, start from content size */
flex: 0 1 auto;    /* Shrink only, start from content size (browser default) */
flex: none;        /* 0 0 auto — fixed size, no flex */

Why It Matters

Flexbox is one of the most used CSS features, but its unintuitive default values (flex-shrink:1, min-width:auto) cause layout bugs that are hard to diagnose. Understanding the flex shorthand and the min-width:0 fix prevents the most common Flexbox breakage scenarios.

Common Patterns

/* Equal-width columns that fill the container */
.nav-items {
  display: flex;
  gap: 1rem;
}
 
.nav-item {
  flex: 1;  /* Equal widths */
}
 
/* Sidebar + main content */
.layout {
  display: flex;
  gap: 2rem;
}
 
.sidebar {
  flex: 0 0 280px;  /* Fixed 280px, no growing or shrinking */
}
 
.main-content {
  flex: 1;  /* Takes all remaining space */
}

The min-width: 0 Fix

/* ❌ Text in flex child doesn't truncate */
.card-container {
  display: flex;
}
 
.card-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  /* Doesn't work! Flex items default to min-width: auto */
}
 
/* ✅ Add min-width: 0 to allow shrinking below content size */
.card {
  flex: 1;
  min-width: 0;  /* Allows flex child to shrink below its content width */
}
 
.card-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  /* Now works correctly */
}

gap vs margin

/* ❌ Using margins for flex item spacing */
.button-group {
  display: flex;
}
 
.button-group > * + * {
  margin-left: 0.5rem;  /* Complex selectors, doesn't work for wrapping */
}
 
/* ✅ Use gap */
.button-group {
  display: flex;
  gap: 0.5rem;  /* Works for both row and column, handles wrapping correctly */
  flex-wrap: wrap;
}

Centering

/* Perfect centering */
.centered {
  display: flex;
  align-items: center;    /* Cross axis */
  justify-content: center; /* Main axis */
}
 
/* Space items evenly */
.spaced {
  display: flex;
  justify-content: space-between; /* Space between items */
  align-items: center;
}
 
/* Push last item to end */
.header {
  display: flex;
  align-items: center;
}
 
.header__actions {
  margin-left: auto; /* Pushes to the right */
}

Wrapping for Responsive Grids

/* Simple responsive grid without media queries */
.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
 
.tag {
  /* Tags take as much space as their content needs */
  flex: 0 0 auto;
}

Verification

  1. Inspect the rendered UI at the breakpoints and interaction states affected by the rule.
  2. Confirm the computed styles match the intended fix in DevTools.
  3. Test at least one mobile and one desktop viewport before shipping.
  4. If the rule affects motion, contrast, or layout stability, verify those user-facing outcomes directly.

Use with AI

Copy these prompts to use with your AI assistant, or install the MCP server to use directly from Claude, Cursor, or Windsurf.

Check

Verify implementation

Review this CSS for common Flexbox mistakes: missing min-width:0 on text-containing flex children, using margin instead of gap for spacing, and improper flex shorthand usage.

Fix

Auto-fix issues

Fix the Flexbox layout issues: add min-width:0 where needed, replace inter-item margins with gap, and correct flex shorthand values.

Explain

Learn more

Explain how flex-grow, flex-shrink, and flex-basis work, what the flex shorthand means, and why min-width:0 fixes text overflow in flex layouts.

Review

Code review

Review stylesheets, component styles, and responsive states related to Apply Flexbox best practices. Flag exact selectors, declarations, or breakpoints that violate the rule in the rendered UI.

Sources

References used to support the guidance in this rule.

Further Reading

Tools and supplementary material for exploring the topic in more depth.

MDN Flexboxdeveloper.mozilla.orgTool

Rules that often go hand-in-hand with this one.

Use CSS Grid for two-dimensional layouts

Use CSS Grid when you need to control both rows and columns simultaneously, such as page layouts, card grids, and complex component arrangements.

CSS
Use relative units for responsive layouts

Use rem, em, %, vw, vh, and clamp() instead of fixed px values to build layouts that scale with user font size preferences and viewport dimensions.

CSS
Use CSS logical properties for i18n and RTL support

Use CSS logical properties (margin-inline, padding-block, border-inline-start) instead of physical properties (margin-left, padding-top) to support right-to-left languages automatically.

CSS
Prevent horizontal scrolling

Web pages must not require horizontal scrolling at standard viewport widths. Horizontal overflow breaks responsive layouts and makes content inaccessible to low-vision users who zoom in.

CSS

Was this rule helpful?

Your feedback helps improve rule quality. This stays internal for now.

Loading feedback...
0 / 385