Skip to content

Courses / CSS3 Mastery: Beginner to Advanced / Advanced Flexbox

2,099 people viewed this

Advanced Flexbox

CSS Grid FundamentalsAdvanced Flexbox

Learning Objectives

  • Use flex-wrap and align-content.
  • Understand flex-grow, flex-shrink and flex-basis.
  • Control individual alignment and order.
  • Build responsive cards and flexible layouts.

1. Wrapping

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.card {
  flex: 1 1 240px;
}

Flex-wrap allows items to form multiple flex lines when necessary. Without wrapping, items generally remain on a single line and may shrink or overflow.

2. The flex Shorthand

.item {
  flex: 1 1 240px;
}
  • flex-grow: How items share positive free space.
  • flex-shrink: How items shrink when negative free space needs distributing.
  • flex-basis: The initial main-size basis before free-space distribution.

The three-value shorthand expresses grow, shrink and basis. A common pattern, flex: 1, distributes available space, subject to flex sizing rules and minimum-size constraints.

3. align-self

.special {
  align-self: flex-end;
}

Align-self overrides the container's align-items value for an individual flex item.

4. align-content

.wrapped {
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
}

Align-content distributes flex lines along the cross axis when multiple lines exist and there is extra cross-axis space. It is not a replacement for align-items.

5. Order

.featured {
  order: -1;
}

Order changes visual ordering among flex items. It does not change the source order used by screen readers and keyboard navigation. Keep the DOM order logical.

6. Automatic Minimum Size

.flex-child {
  min-width: 0;
}

Flex items can have an automatic minimum size that prevents them from shrinking as much as expected. Setting min-width: 0 can allow text or content to shrink within a row layout. Use overflow-wrap when long words need to break.

7. Responsive Card Layout

.cards {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}
.card {
  flex: 1 1 260px;
  min-width: 0;
}

Rate this course

No ratings yet — yours can be the first.

Stars show on this course. Your written note stays private.

Your stars