KodingKaro
CSS · Intermediate · Flexbox

Flex sizing, wrapping, and practical patterns

Flex items can grow, shrink, and wrap. Those controls help toolbars and navigation adapt when labels or available width change.

✓ What you will learn in this topic

  • Explain the key idea: Flex sizing controls growth and shrinkage; wrapping supports changing available space.
  • Apply the concept: The demo items wrap into additional rows when space is limited instead of overflowing the container.
  • Avoid this common mistake:

Concept explanation

The flex shorthand combines grow, shrink, and basis. A basis is the starting size before free space is distributed. flex-wrap: wrap lets items move to another line. A child’s default minimum size can sometimes prevent shrinking; min-width: 0 can allow long content to fit its track.

Do not force a navigation row to remain on one line at every viewport. Use wrapping, a different layout, or a deliberate mobile navigation pattern.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}
.demo > * {
  flex: 1 1 10rem;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}
.demo > * {
  flex: 1 1 10rem;
}

Expected result: The demo items wrap into additional rows when space is limited instead of overflowing the container.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card .actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}
.card h2 {
  min-width: 0;
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Applying flex properties to a child instead of its flex container, or expecting Flexbox to create a two-dimensional grid.

Best practice

Use gap for spacing, allow items to wrap when needed, and define sensible flex-basis or minimum sizes.

Practice exercise

Build a header with a brand, navigation links, and a call-to-action that wraps cleanly on a narrow screen. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Flex sizing controls growth and shrinkage; wrapping supports changing available space.
  • Use gap for spacing, allow items to wrap when needed, and define sensible flex-basis or minimum sizes.

Knowledge check

What does flex-wrap: wrap allow?

Reveal the explanation

Wrapping lets the layout adapt to limited width rather than forcing one crowded row.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}
.demo > * {
  flex: 1 1 10rem;
}

Expected result:

The demo items wrap into additional rows when space is limited instead of overflowing the container.

Hands-on practice task

Build a header with a brand, navigation links, and a call-to-action that wraps cleanly on a narrow screen.

In this section

Other lessons in Flexbox

Broaden your knowledge

Related learning topics