Skip to content

Search

Articles, tools, solutions and knowledge base guides

Start typing to search across articles, tools and apps.

CSS Flexbox: A Practical Guide with Examples

Flexbox distributes items along one direction and adapts their sizes to available space. Rather than memorizing properties in isolation, understand how the browser resolves sizes and distributes free space. These examples progress from a simple action bar to responsive forms and independently scrolling panels.

Published Written by Reading time 8 min read
On this page Contents: quick navigation between sections

Flex containers and axes

<nav class="navigation" aria-label="Main navigation">
  <a href="/en/products">Products</a>
  <a href="/en/docs">Documentation</a>
  <a href="/en/support">Support</a>
</nav>
.navigation {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
}

display: flex establishes a container whose direct in-flow children become flex items. With the default flex-direction: row, the main axis follows the inline direction and the cross axis is perpendicular. column uses the block direction as its main axis. Do not assume these axes always correspond to horizontal and vertical. [1]

The navigation example remains on one line while space permits. gap separates items without adding margins at the outer edges. flex-wrap allows additional lines in narrow containers. [2]

How flex-grow, flex-shrink and flex-basis work

<div class="growth-demo">
  <div class="growth-demo__primary">200px</div>
  <div class="growth-demo__secondary">400px</div>
</div>

<div class="shrink-demo">
  <div class="shrink-demo__primary">240px</div>
  <div class="shrink-demo__secondary">160px</div>
</div>
.growth-demo {
  display: flex;
  inline-size: 640px;
  gap: 40px;
}

.growth-demo__primary {
  flex: 1 1 100px;
  min-inline-size: 0;
}

.growth-demo__secondary {
  flex: 3 1 100px;
  min-inline-size: 0;
}

.shrink-demo {
  display: flex;
  inline-size: 400px;
}

.shrink-demo__primary {
  flex: 0 1 300px;
  min-inline-size: 0;
}

.shrink-demo__secondary {
  flex: 0 1 200px;
  min-inline-size: 0;
}

flex-basis supplies the starting main size, flex-grow allocates positive free space, and flex-shrink participates in reducing sizes when space is insufficient. The initial values correspond to flex: 0 1 auto. Browsers typically expand flex: 1 to 1 1 0%; explicit 1 1 0 avoids the difference between zero length and a percentage in an indefinite container. [3][4]

With a 640px container, a 40px gap and two 100px bases, 400px remains. Grow factors 1 and 3 add 100px and 300px, yielding 200px and 400px if no other constraints apply. Shrinking is weighted by base size: bases of 300px and 200px, a 100px deficit and shrink factors of 1 yield 240px and 160px unless minimum sizes intervene. [4]

Alignment, centering and auto margins

<div class="action-bar">
  <button type="button">Filter</button>
  <button type="button">Sort</button>
  <button class="action-bar__export" type="button">Export</button>
</div>

<div class="empty-state">
  <p>No results</p>
</div>
.action-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
}

.action-bar__export {
  margin-inline-start: auto;
}

.empty-state {
  display: flex;
  min-block-size: 12rem;
  align-items: center;
  justify-content: center;
}

justify-content distributes space on the main axis, align-items aligns items on the cross axis, and align-self overrides alignment for one item. align-content applies to line packing in a multi-line flex container; line centering becomes visible when there is spare cross-axis space. [5]

In the action bar, margin-inline-start: auto pushes the last button toward the end of the row when positive space remains. Main-axis auto margins absorb that space before justify-content. Center a single panel's content using justify-content: center and align-items: center with sufficient panel height. [5]

Wrapping and responsive forms

<form class="profile-form">
  <label class="profile-form__field">
    <span>First name</span>
    <input name="givenName" autocomplete="given-name" />
  </label>

  <label class="profile-form__field">
    <span>Last name</span>
    <input name="familyName" autocomplete="family-name" />
  </label>

  <label class="profile-form__field">
    <span>Email</span>
    <input name="email" type="email" autocomplete="email" />
  </label>
</form>
.profile-form {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.profile-form__field {
  display: flex;
  flex: 1 1 14rem;
  flex-direction: column;
  min-inline-size: 0;
  gap: 0.5rem;
  overflow-wrap: anywhere;
}

.profile-form__field input {
  box-sizing: border-box;
  inline-size: 100%;
  min-inline-size: 0;
  padding: 0.75rem;
}

Flexbox distributes items separately in each line. A final line containing only one growing item can therefore fill the whole width. If columns must align across rows, Grid is a better starting point. [2][6]

The form uses fields with a 14rem basis that may grow. Wrapping follows field sizes and gaps without a manually chosen viewport breakpoint. min-inline-size: 0 permits shrinking and controls fill the available width. Long labels can wrap.

Why a flex item refuses to shrink

<div class="file-row">
  <div class="file-row__details">
    <span class="file-row__name">
      customer-invoices-international-archive-final.pdf
    </span>
  </div>

  <a class="file-row__action" href="/en/files/42">Details</a>
</div>
.file-row {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.file-row__details {
  flex: 1 1 auto;
  min-inline-size: 0;
}

.file-row__name {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-row__action {
  flex: 0 0 auto;
}

The default automatic main-axis minimum is often content-based. A long identifier, image or text with white-space: nowrap can push adjacent items away even with flex-shrink: 1. In a row, applying min-inline-size: 0 to the relevant item removes that particular constraint; in a column use min-block-size: 0. [4]

Ellipsis in the file row additionally needs constrained width, overflow: hidden, white-space: nowrap and text-overflow: ellipsis. The full name remains in the DOM text; provide a way for sighted users to read it too, such as a details view. Do not conceal information essential to a task.

Scrollable panels and card footers

<section class="activity-panel" aria-labelledby="activity-title">
  <header>
    <h2 id="activity-title">Activity</h2>
  </header>

  <div
    class="activity-panel__body"
    tabindex="0"
    role="region"
    aria-labelledby="activity-title"
  >
    <ul>
      <li>Order created</li>
      <li>Payment confirmed</li>
      <li>Package dispatched</li>
    </ul>
  </div>

  <footer>End of activity</footer>
</section>

<div class="product-cards">
  <article class="product-card">
    <h2>Starter</h2>
    <p>Basic features for small teams.</p>
    <footer class="product-card__footer">
      <a href="/en/plans/starter">View plan</a>
    </footer>
  </article>

  <article class="product-card">
    <h2>Professional</h2>
    <p>Advanced reporting and additional integrations for larger teams.</p>
    <footer class="product-card__footer">
      <a href="/en/plans/professional">View plan</a>
    </footer>
  </article>
</div>
.activity-panel {
  display: flex;
  flex-direction: column;
  block-size: 18rem;
  gap: 1rem;
}

.activity-panel__body {
  flex: 1 1 0;
  min-block-size: 0;
  overflow: auto;
}

.product-cards {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
  gap: 1rem;
}

.product-card {
  display: flex;
  flex: 1 1 16rem;
  flex-direction: column;
  min-inline-size: 0;
  gap: 1rem;
}

.product-card__footer {
  margin-block-start: auto;
}

In a column layout, the middle item often needs to occupy remaining height and scroll independently. Give the panel a constrained block size and the scroll region flex: 1 1 0, min-block-size: 0 and overflow: auto. Nested layouts may require removing automatic minimums on intermediate flex items too. [4]

The same free-space mechanism helps cards: set display: flex and flex-direction: column on the card and margin-block-start: auto on its footer. Footers align when cards in the same row share a height. Separate flex lines do not automatically align with one another.

DOM order, RTL and accessibility

<nav class="account-nav" aria-label="Account">
  <a href="/en/account">Overview</a>
  <a href="/en/account/settings">Settings</a>
  <a class="account-nav__help" href="/en/support">Help</a>
</nav>
.account-nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
}

.account-nav__help {
  margin-inline-start: auto;
}

.account-nav a:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 4px;
}

order, row-reverse and column-reverse alter visual layout but not document source order. Do not use them to repair incorrect reading or focus order. A mismatch can disorient keyboard and assistive-technology users. [4][7]

Keep navigation links in logical HTML order. Logical properties such as margin-inline-start follow text direction, avoiding manual reversal for RTL. Avoid positive tabindex values as a workaround for layout problems.

Debugging Flexbox in browser tools

When layout behaves unexpectedly, inspect the main axis, computed flex-basis, flex-grow, flex-shrink, minimum and maximum sizes and available space. The Flexbox DevTools overlay reveals boundaries and gaps, but the cause of overflow often becomes clear only in computed styles. [8]

Test long names, enlarged text, RTL and narrow containers. Do not add overflow: hidden to the whole page merely to conceal horizontal scrolling; fix its source. Choose Grid for shared row and column tracks, and Flexbox for flexible distribution in one direction. [6]

Flexbox excels when items need to respond to available space in one direction. Understanding axes, base sizes and automatic minimums solves most problems without artificial widths or excessive breakpoints. Grid complements it when shared tracks in two dimensions are required.

Tools for this topic

CSS Flexbox CSS Layout Responsive Web Design flex-grow flex-shrink flex-basis Accessibility DevTools

Frequently asked questions

Is Flexbox two-dimensional?

Items can wrap into multiple lines, but each line distributes space independently. Grid provides shared row and column tracks. [6]

Why does justify-content not work?

There may be no free main-axis space because growing items or auto margins already consumed it. Check the axis direction too. [5]

Does flex: 1 guarantee equal widths?

Not always. Content-based minimums, padding, borders and other constraints can influence the result. [4]

When should I use min-width: 0?

When a horizontal flex item cannot shrink due to its automatic minimum. Use min-inline-size: 0 for writing-mode-aware row layouts. [4]

Is Flexbox faster than Grid?

Neither has a universal advantage. Choose the model that matches the layout and measure actual rendering cost.

Sources and references

  1. MDN, Basic concepts of flexbox↩
  2. MDN, Mastering wrapping of flex items↩1↩2
  3. MDN, flex↩
  4. W3C, CSS Flexible Box Layout Module Level 1↩1↩2↩3↩4↩5↩6↩7
  5. MDN, Aligning items in a flex container↩1↩2↩3
  6. MDN, Relationship of flexbox to other layout methods↩1↩2↩3
  7. MDN, Ordering flex items↩
  8. Chrome DevTools, Inspect and debug CSS flexbox layouts↩

Was this helpful?

Get new articles by email

One short email per new Learning article. No spam, unsubscribe in one click.

We only use your email to send new articles. No third-party sharing.

Back to Learning