Learning path · Level 3 · Tutorial 1

Arrange Content with Flexbox

Build flexible rows and columns that align, distribute space, and wrap while preserving meaningful reading and keyboard order.

  • Level: Beginner
  • Duration: 20 min
  • 9 steps
Start tutorial Back to the path
Arrange Content with Flexbox

What you will go through

  1. Create a Flex Container Turn the Resource library card wrapper into a flex container and identify its direct flex items. 2 min
  2. Read the Main and Cross Axes Identify Flexbox’s main and cross axes, then choose row or column for a requested card arrangement. 2 min
  3. Align Items and Set Gaps Choose container properties that position flex items on each axis and create consistent gutters between cards. 3 min
  4. Let Cards Wrap onto New Lines Let the Resource library cards create additional flex lines as their available width becomes too narrow. 2 min
  5. Set a Starting Size and Share Extra Space Use flex-basis and flex-grow to start resource cards at a preferred size and distribute positive free space proportionally. 2 min
  6. Control Shrinking with the Flex Shorthand Control whether Resource library cards grow, shrink, and start from a preferred size by reading and writing the complete flex shorthand. 2 min
  7. Remove an Unwanted Minimum-Width Constraint Allow a constrained flex item to shrink while preserving readable, fully visible heading text. 3 min
  8. Keep Visual and Document Order Together Keep Flexbox presentation aligned with the meaningful source and keyboard sequence. 2 min
  9. Build and Check the Flexible Resource Library Build and locally verify a wrapping Flexbox resource library with readable content and a meaningful keyboard sequence. 4 min

What you will learn

  • Create a flex container and identify its main and cross axes.
  • Align and space items using justify-content, align-items, and gap.
  • Control item growth, shrinking, and wrapping as available width changes.
  • Correct a flex item's minimum-size constraint without hiding its content.
  • Keep the visual arrangement consistent with document reading and keyboard focus order.

Before you start

  • Apply CSS with Stylesheets and Selectors
  • Control Element Size and Spacing
  • Organize Content with Semantic HTML

Step 1 of 9

Create a Flex Container

Turn the Resource library card wrapper into a flex container and identify its direct flex items.

Starter files

Set up the local example

Create a folder for this example and add two files beside each other: index.html and styles.css. The HTML links to the stylesheet and provides the Resource library content used throughout this tutorial.

Create index.html

index.html

html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Resource library</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <main>
    <h1>Resource library</h1>

    <div class="resources">
      <article class="resource-card resource-card--featured">
        <h2 class="resource-title">HTML reference</h2>
        <p>Review the elements that give page content meaning.</p>
        <a href="#html-notes">Read HTML notes</a>
      </article>

      <article class="resource-card">
        <h2 class="resource-title">CSS layout notes</h2>
        <p>Use layout rules to arrange content clearly.</p>
        <a href="#css-notes">Read CSS notes</a>
      </article>

      <article class="resource-card">
        <h2 class="resource-title">Accessibility checklist</h2>
        <p>Check that content remains usable for everyone.</p>
        <a href="#accessibility-notes">Read accessibility notes</a>
      </article>
    </div>

    <section id="html-notes">
      <h2>HTML reference</h2>
      <p>Semantic elements describe the purpose of content.</p>
    </section>

    <section id="css-notes">
      <h2>CSS layout notes</h2>
      <p>Layout rules control how content uses available space.</p>
    </section>

    <section id="accessibility-notes">
      <h2>Accessibility checklist</h2>
      <p>Meaningful order supports reading and keyboard navigation.</p>
    </section>
  </main>
</body>
</html>
Expected output
Browser result: a page titled Resource library with the three resource cards followed by their matching destination sections.

After you also save the starter stylesheet beside this file and open or refresh index.html, the browser displays the Resource library heading, three outlined cards, and three destination sections below them.

Create styles.css

styles.css

css
body {
  font-family: system-ui, sans-serif;
  margin: 0;
  padding: 2rem;
}

.resources {
  border: 0.125rem solid #94a3b8;
  padding: 1rem;
}

.resource-card {
  border: 0.125rem solid #cbd5e1;
  padding: 1rem;
}

.resource-title {
  margin-top: 0;
}
Expected output
Browser result: the Resources wrapper has an outer border. HTML reference, CSS layout notes, and Accessibility checklist are separate outlined cards stacked vertically.

This is the starting, non-Flexbox view. At a typical wide browser width, the cards are block elements and appear one below another inside the outlined wrapper.

Turn on Flexbox

One container, three flex items

Flexbox is a one-dimensional layout system: it arranges flex items along one main axis, making it useful for rows and columns.

Add display: flex to .resources. This element becomes the flex container. Its three direct <article> children become flex items.

A heading, paragraph, or link inside an article is nested content. It does not automatically become a flex item of .resources.

See the parent-child relationship

Diagram of a resources flex container holding three direct article card flex items, each with nested heading, paragraph, and link content.

Only the three direct article children become flex items of the Resources flex container.

Make the wrapper a flex container

Replace the existing .resources rule in styles.css with this rule, then save and refresh the browser.

css
.resources {
  display: flex;
  border: 0.125rem solid #94a3b8;
  padding: 1rem;
}
Expected output
Browser result: HTML reference, CSS layout notes, and Accessibility checklist appear side by side as three flex items inside the Resources flex container.

At a typical wide browser width, the three cards now occupy one line within the bordered wrapper. Their headings, paragraphs, and links remain inside their own cards.

Practice: direct children matter

Identify the container and its items

Complete the container declaration and the selector for the three direct card elements. Do not target their nested headings or links.

css
.resources {
  display: ;
}

.resources >  {
  border-color: #0f766e;
}

Step 2 of 9

Read the Main and Cross Axes

Identify Flexbox’s main and cross axes, then choose row or column for a requested card arrangement.

See how direction defines the axes

Two axes, one chosen direction

A flex container has two directions: the main axis, where its flex items are arranged, and the cross axis, which is perpendicular to it.

In this tutorial’s left-to-right horizontal writing:

  • flex-direction: row is the default. The main axis runs horizontally; the cross axis runs vertically.
  • flex-direction: column makes the main axis run vertically; the cross axis runs horizontally.

“Main” means the direction selected for arranging items. It does not always mean horizontal.

Axes change with direction

Comparison diagram showing a horizontal main axis and vertical cross axis for a row, then a vertical main axis and horizontal cross axis for a column.

Changing the flex direction changes which physical direction is the main axis.

Use column for a stack

Choose a vertical arrangement

The Resource library cards are direct children of .resources, so flex-direction controls whether those cards form a row or a vertical stack. You do not need to change their HTML sequence.

Because row is the default, add flex-direction only when you want the stacked arrangement.

CSS for a card stack

Add this declaration to the existing .resources rule when the cards should stack from top to bottom. Save styles.css, then refresh your local browser.

css
.resources {
  display: flex;
  flex-direction: column;
}
Expected output
Browser result: the HTML reference card appears first, the CSS layout notes card appears below it, and the Accessibility checklist card appears last. The cards form one vertical stack inside the bordered resources container.

The existing border and padding on `.resources`, plus the border and padding on each card, remain in effect from the previous step.

Practice reading axis diagrams

Identify the axes

Match each axis label to its direction in a left-to-right horizontal writing layout.

Tap an item and then its matching pair.

Apply the direction

Stack the cards

Complete the declaration that stacks the resource cards vertically without changing their HTML order.

css
.resources {
  display: flex;
  flex-direction: ;
}

Step 3 of 9

Align Items and Set Gaps

Choose container properties that position flex items on each axis and create consistent gutters between cards.

Position cards along the row

Use free space on the main axis

With flex-direction: row, the main axis runs horizontally. If the cards occupy less than the container’s available width, the remainder is free space. justify-content positions the card group or distributes that free space along the main axis.

It affects flex items, not the text inside a card. The comparison below uses the same three cards and the same container; only the handling of main-axis free space changes.

Three main-axis arrangements

Three diagrams compare cards placed at the start, in the middle, and distributed across a horizontal flex container.

The container and cards remain the same; justify-content changes where main-axis free space goes.

Compare main-axis values

Choose the value that matches the intended result.

Effects in a row flex container that has horizontal free space
ValueCard placementFree-space result
flex-startAt the left main-axis edgeAfter the last card
centerIn the horizontal middleSplit across both outer sides
space-betweenFirst and last cards at opposite edgesDistributed between cards

Control the cross axis

Align cards across the row

align-items works on the cross axis. In a row, that axis is vertical.

  • stretch is the default. A flex item with an automatic cross-axis size can stretch to the flex line’s cross size.
  • flex-start places cards at the top cross-axis edge.
  • center places cards halfway between the top and bottom edges.

Stretch is most visible when cards have automatic heights. It does not override an explicit cross-axis size. A container also needs extra cross-axis space before top or center placement becomes visible.

Three cross-axis arrangements

Three diagrams compare stretched cards, cards aligned at the top, and cards aligned in the vertical middle of a tall row container.

For a row, align-items changes vertical placement or stretching.

Restore a row and center its cards

Replace the earlier .resources and .resource-card rules with these temporary comparison rules. flex-direction: row explicitly replaces the column direction used in the previous step. The fixed height only makes cross-axis centering easy to inspect.

css
.resources {
  display: flex;
  flex-direction: row;
  width: 48rem;
  height: 14rem;
  justify-content: flex-start;
  align-items: center;
  gap: 1rem;
  border: 0.125rem solid #94a3b8;
  padding: 1rem;
}

.resource-card {
  width: 12rem;
  border: 0.125rem solid #cbd5e1;
  padding: 1rem;
}
Expected output
Browser result: the Resource library is a horizontal row inside a 14rem-tall content area. The cards keep their automatic content-based heights and sit vertically in the middle instead of stretching from the top inner edge to the bottom inner edge.

The existing `.resource-title` rule remains in effect. Exact text wrapping can vary slightly with browser fonts.

Preview a column arrangement

Three equally wide cards are stacked near the top center of a tall container, with equal gutters and substantial unused space below.

This target will be recreated after you examine how gutters differ from outer container space.

Add predictable gutters

Separate gutters from outer space

gap creates a gutter between neighboring flex items. It does not add space between the first or last card and the container edge; container padding provides that outer space.

A declared gap remains present with space-between. However, space-between also distributes any remaining main-axis free space between cards. The visible separation can therefore be larger than the declared gap.

Gap plus distributed space

Three cards have outer inset space from container padding and between-card spaces made larger by both gutters and distributed free space.

A gap is between items. Main-axis distribution can add more visible space between those same items.

Combine distribution, alignment, and a gutter

Replace the temporary comparison rules with this combined row arrangement. It keeps the explicit row direction, so the first property controls horizontal distribution and the second controls vertical alignment.

css
.resources {
  display: flex;
  flex-direction: row;
  width: 48rem;
  height: 14rem;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  border: 0.125rem solid #94a3b8;
  padding: 1rem;
}

.resource-card {
  width: 12rem;
  border: 0.125rem solid #cbd5e1;
  padding: 1rem;
}
Expected output
Browser result: the three cards form a horizontally distributed row and are vertically centered in the 14rem-tall content area. The first and last cards meet the padded inner edges. Each visible separation between neighboring cards is 2.625rem: the 1rem gap plus 1.625rem of distributed free space.

This calculation uses the existing content-box sizing: each card’s outer width is 14.25rem, including its padding and border.

Tip

Use one property for each job

Keep the responsibilities separate:

  • justify-content places or distributes items on the main axis.
  • align-items positions items on the cross axis.
  • gap creates the recurring space between neighboring items.

When direction changes to column, these properties still use the main and cross axes; the physical directions change with the axes.

Apply axis-based alignment

Complete the column alignment recipe

Use the column target shown earlier. Complete the container recipe for a top-centered vertical stack with a recurring gutter. The 48rem content height is large enough for the three actual cards, their two gutters, and unused space below.

css
.resources {
  display: flex;
  height: 48rem;
  : ;
  : ;
  : center;
  : 1rem;
  border: 0.125rem solid #94a3b8;
  padding: 1rem;
}

.resource-card {
  width: 12rem;
  border: 0.125rem solid #cbd5e1;
  padding: 1rem;
}

Step 4 of 9

Let Cards Wrap onto New Lines

Let the Resource library cards create additional flex lines as their available width becomes too narrow.

See the default behavior

Keep the cards on one line

One line is the default

A flex container uses one flex line by default: flex-wrap: nowrap. In a row, this keeps all three flex items on the same line. It does not promise that an authored width remains unchanged: when the line is too narrow, items may become narrower within their size constraints rather than move to another line.

For a controlled comparison, give .resources a 30rem content width. The existing cards use the default content-box sizing. Each width: 12rem card also has 1rem padding and 0.125rem borders on both sides, so its outer width is 14.25rem.

The default does not add a line

A comparison of three cards in a roomy row and the same three cards compressed into one narrower row.

With the default nowrap behavior, a narrower flex container still keeps its direct children on one flex line.

Set up a no-wrap comparison

For this comparison, replace the current .resources and .resource-card rules with these temporary rules. The 30rem width is the container's content width; its padding and border remain outside that measurement.

css
.resources {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  width: 30rem;
  border: 0.125rem solid #94a3b8;
  padding: 1rem;
  gap: 1rem;
}

.resource-card {
  width: 12rem;
  border: 0.125rem solid #cbd5e1;
  padding: 1rem;
}
Expected output
Browser result: HTML reference, CSS layout notes, and Accessibility checklist remain on one horizontal flex line. No card starts a second line; the cards can be narrower than their authored 12rem content widths.

View this at a browser width that can display the 30rem container and its outer padding. The exact compressed card widths can vary with content, but no second card line is created.

Wrap cards onto a new line

Wrap when available width runs out

Permit another flex line

Add flex-wrap: wrap when cards should form additional lines instead of remaining on one crowded row.

Here, two card outer widths require 28.5rem (14.25rem each). With their 1rem gutter, the first line needs 29.5rem, so two cards fit in the 30rem available content width. A third card does not fit and starts a second line.

gap works in both directions: it separates neighboring cards on a line and separates wrapped lines. Each line is laid out independently. With justify-content: flex-start, the lone final card begins at the main-axis start; it does not reserve an empty shared column beside it.

Enable natural wrapping

Add the wrapping declaration to the temporary .resources rule, then save styles.css and refresh your browser.

css
.resources {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: flex-start;
  width: 30rem;
  border: 0.125rem solid #94a3b8;
  padding: 1rem;
  gap: 1rem;
}

.resource-card {
  width: 12rem;
  border: 0.125rem solid #cbd5e1;
  padding: 1rem;
}
Expected output
Browser result: HTML reference and CSS layout notes appear on the first line. Accessibility checklist starts a second line at the left main-axis edge. A 1rem gap separates the cards and the two lines.

This result uses the existing card padding and borders, so each card has a 14.25rem outer width. The 1rem gap is visible both between the first two cards and between the two lines.

Lines are independent

Two cards fill the first flex line, while one card begins a second line with open space to its right.

Wrapping creates independent flex lines. The final line has one card at its start, not a blank placeholder that aligns it into a shared column.

Apply the wrapping rule

Predict the wrapped flow

Complete the declaration for .resources, then predict the layout at a 30rem available content width. Each card has an outer width of 14.25rem, and the gap is 1rem.

State how many cards appear on the first line, how many appear on the final line, and where the final-line card begins.

Write at least 55 characters (0/55).

Step 5 of 9

Set a Starting Size and Share Extra Space

Use flex-basis and flex-grow to start resource cards at a preferred size and distribute positive free space proportionally.

Preferred size is not final size

Start from a preferred card size

flex-basis gives each flex item a preferred starting size on the main axis. In this row layout, 12rem is the preferred starting width for each resource card.

It is a starting point, not a promise about the final width. When a flex line has positive free space, flex-grow determines whether—and by what proportion—items receive some of that space.

See starting size and added space

Diagram showing three cards with equal starting widths and different added widths, plus a wrapped last-line card expanding independently.

Equal pale areas represent preferred starting sizes. Colored extensions represent positive free space distributed on each flex line.

Share available width

Growth factors are shares

Use flex-grow only to describe how positive free space is shared on one flex line. A value of 0 leaves an item out of growth. Positive values are relative shares: a factor of 2 receives twice the added space of a factor of 1.

This ratio applies to added space—not to final card widths. If every card starts at 12rem, the featured card’s final width includes its own 12rem starting size plus its larger added share. Wrapped lines resolve their available space independently, so a card alone on a last line can grow.

Compare growth values

Compare the meaning of common growth factors when a line has positive free space.

Growth factors describe shares of additional main-axis space on the same flex line.
flex-grow valueResult when space is available
0Receives no added space through growth
1Receives one relative share
2Receives two relative shares
1 and 2 togetherThe factor 2 item gets twice each factor 1 item’s added share

Give cards a basis and growth weights

Replace the temporary .resources and .resource-card rules from the wrapping comparison with these demonstration rules. The first card already has the resource-card--featured class.

css
.resources {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 48rem;
  border: 0.125rem solid #94a3b8;
  padding: 1rem;
  gap: 1rem;
}

.resource-card {
  flex-basis: 12rem;
  flex-grow: 1;
  border: 0.125rem solid #cbd5e1;
  padding: 1rem;
}

.resource-card--featured {
  flex-grow: 2;
}
Expected output
Browser result: the three cards appear on one row at the 48rem content width. All begin from the same preferred width, then the featured HTML reference card receives a larger portion of the remaining horizontal space than either ordinary card. The cards remain in document order.

The exact pixel widths depend on the browser’s root font size, but the relative result is the same.

Configure the cards

Complete the sizing rules

Complete the preferred-size and growth configuration for the Resource library cards.

css
.resource-card {
  flex-basis: ;
  flex-grow: ;
}

.resource-card--featured {
  flex-grow: ;
}

Interpret added space

Interpret the comparison

A wide flex line contains three cards. Each has flex-basis: 12rem; the featured card has flex-grow: 2; and each ordinary card has flex-grow: 1. Which interpretation is accurate?

Step 6 of 9

Control Shrinking with the Flex Shorthand

Control whether Resource library cards grow, shrink, and start from a preferred size by reading and writing the complete flex shorthand.

Decide whether items may shrink

Shrink only matters when space is insufficient

When a flex line runs short of space

flex-shrink controls whether a flex item participates in becoming smaller when the items on one flex line do not fit.

  • flex-shrink: 0 keeps that item out of shrinkage.
  • flex-shrink: 1 lets it shrink, subject to minimum-size constraints.

A shrink factor is a participation factor, not a number of pixels. Two items with the same factor can lose different pixel amounts when their starting sizes differ.

How shrinking responds to a narrow line

A narrow flex row before and after three differently sized cards shrink; the card that started wider becomes smaller by a larger visible amount.

Equal shrink factors do not require equal pixel reductions: an item's starting size also affects shrink distribution.

Tip

Keep wrapping and shrinking separate

flex-wrap and flex-shrink solve different decisions. Wrapping permits another flex line. Shrinking adjusts sizes on a line that has insufficient space. You may use both properties together, but a shrink factor does not turn wrapping on.

Compare sizing policies

Read the three-part shorthand

The flex shorthand lists three components in this order:

grow → shrink → basis

The basis is the preferred starting main-axis size used in the previous step. The first two numbers state whether an item can receive positive free space or help resolve a shortage of space.

Compare complete flex values

These complete declarations express different sizing policies.

Each row is written as grow, shrink, basis.
DeclarationGrowthShrinkStarting basis
flex: 0 1 autoNoYesauto
flex: 0 0 12remNoNo12rem
flex: 1 1 12remYesYes12rem

Apply two sizing policies

For a temporary comparison, replace the sizing declarations from the previous step with these rules. Keep the existing .resources rule, including its 48rem width, wrapping, and 1rem gap.

css
.resource-card {
  flex: 1 1 12rem;
}

.resource-card--featured {
  flex: 0 0 12rem;
}
Expected output
Browser result after saving and refreshing:
The three cards fit on one row at the earlier 48rem container width. The featured HTML reference card stays at its 12rem basis and does not take extra width or shrink. The two ordinary cards may share the remaining positive free space and are wider than their 12rem basis.

Exact final widths vary with the container's available width and the cards' padding and borders. The important result is which cards receive extra space.

Check the behavior of each value

Interpret the shorthand

Match each complete flex declaration to its behavior.

Tap an item and then its matching pair.

Apply the flex shorthand

Write a complete flex declaration

Complete the declaration for resource cards that may both grow and shrink from a preferred 12rem basis.

css
.resource-card {
  flex:   ;
}

Step 7 of 9

Remove an Unwanted Minimum-Width Constraint

Allow a constrained flex item to shrink while preserving readable, fully visible heading text.

Notice the automatic minimum width

Why a shrinkable card can remain too wide

A card can be allowed to shrink and still stop sooner than expected. In this row, flex: 1 1 12rem permits shrinking, but a flex item also has an automatic minimum width based on its content.

A non-wrapping heading makes that content-based minimum wider because its whole phrase must stay on one line. At the 42rem test width below, a card box can therefore resist shrinking and extend the row beyond its container. This is a box-size constraint; it does not mean that Flexbox ignored flex-shrink.

Three distinct narrow-row states

Three-panel diagram. First, a card box resists shrinking and extends outside a narrow flex container. Second, narrower card boxes fit but a single-line heading extends outside one card. Third, the headings wrap visibly inside the narrow cards.

From left to right: an automatic item minimum holds a card box open; min-width: 0 shrinks the box but does not change no-wrap text; normal wrapping completes the readable repair.

Create the constrained row

For a temporary local comparison, replace the current .resources, .resource-card, and .resource-card--featured rules with these rules. The 42rem content width is narrower than the three cards' preferred outer widths plus two gutters, but is wide enough for the longest individual heading word after normal wrapping is restored.

css
.resources {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  width: 42rem;
  border: 0.125rem solid #94a3b8;
  padding: 1rem;
  gap: 1rem;
}

.resource-card {
  flex: 1 1 12rem;
  border: 0.125rem solid #cbd5e1;
  padding: 1rem;
}

.resource-card--featured {
  flex: 1 1 12rem;
}

.resource-title {
  white-space: nowrap;
}
Expected output
Browser result: the three cards remain on one row. Their headings remain on one line, and a card box can keep the row wider than the 42rem container content width rather than producing a readable narrow-card layout.

Expected browser result after saving styles.css and refreshing index.html.

Make shrinking readable

Repair the box and the text separately

Repair the box and the text as separate concerns. Apply min-width: 0 to the constrained flex item—here, each .resource-card. This permits its box to become narrower than its automatic minimum width.

That change alone does not make a non-wrapping heading readable: the box can shrink while its heading still paints outside it. Restore white-space: normal so ordinary heading words can wrap within the smaller card.

Removing a no-wrap rule can resolve some layouts by itself. Conversely, min-width: 0 is a targeted flex-item safeguard, not a universal fix for every kind of overflow.

Allow shrinking and normal wrapping

Keep the temporary .resources rule from the previous screen. Replace the card and title rules with these readable-shrink rules.

css
.resource-card {
  flex: 1 1 12rem;
  min-width: 0;
  border: 0.125rem solid #cbd5e1;
  padding: 1rem;
}

.resource-card--featured {
  flex: 1 1 12rem;
}

.resource-title {
  white-space: normal;
}
Expected output
Browser result: all three card boxes can shrink within the narrow row. Multi-word headings use additional lines as needed, and the heading text remains fully visible inside its card.

Expected browser result after saving styles.css and refreshing index.html at the same 42rem test width.

Warning

Keep essential content visible

Do not hide essential headings with overflow: hidden or truncation merely to make a narrow row look tidy. A heading that wraps remains understandable; a clipped heading removes information from the reader.

Practice the two-part fix

Complete the two-part repair

Complete the two declarations for a card that must shrink at the narrow test width while keeping its heading readable.

css
.resource-card {
  flex: 1 1 12rem;
  min-width: ;
}

.resource-title {
  white-space: ;
}

Apply the repair without hiding content

Choose the complete readable repair

At a test width that accommodates each longest individual heading word after wrapping is enabled, choose the rule pair that explicitly removes the affected flex item's automatic minimum-width barrier, keeps its heading readable, and does not hide essential content.

Step 8 of 9

Keep Visual and Document Order Together

Keep Flexbox presentation aligned with the meaningful source and keyboard sequence.

Document order remains underneath

One source sequence, two experiences

The HTML source sequence is the document order. It provides the meaningful reading sequence and, for the cards’ native links, the expected sequence when a keyboard user presses Tab.

Flexbox can change where cards appear without changing that source sequence. When visual placement differs from document order, a person reading left to right may encounter cards in a different sequence from a keyboard user.

Visual position can disagree with focus order

A diagram compares the document order HTML reference, CSS layout notes, Accessibility checklist with a visually reversed row. The keyboard focus path still starts with HTML reference at the right, then moves to CSS layout notes and Accessibility checklist.

Flexbox can change presentation without rewriting document order.

How visual and keyboard sequences diverge

Visual reordering is not source reordering

Two Flexbox features can create a mismatch:

  • flex-direction: row-reverse reverses visual placement along a row.
  • flex-direction: column-reverse reverses visual placement in a column.
  • order changes a flex item’s visual placement relative to its siblings.

None of them moves an <article> in the HTML. Native link focus still follows the source. Avoid visual reordering when it would make the page’s meaningful sequence unclear.

Try a reversed row

Temporary comparison: add this to the current .resources rule, save, and refresh your browser.

css
.resources {
  display: flex;
  flex-direction: row-reverse;
  gap: 1rem;
}
Expected output
Visual row, left to right: Accessibility checklist, CSS layout notes, HTML reference.

After reloading, Tab reaches Read HTML notes, then Read CSS notes, then Read accessibility notes.

Expected browser result after saving the CSS. The existing width, card sizing, and other declarations remain in effect.

Try an order override

Separate comparison: first restore flex-direction: row, then temporarily add this rule, save, and refresh. Do not combine this comparison with row-reverse.

css
.resource-card--featured {
  order: 1;
}
Expected output
Visual row, left to right: CSS layout notes, Accessibility checklist, HTML reference.

After reloading, Tab still reaches Read HTML notes, then Read CSS notes, then Read accessibility notes.

Expected browser result after saving the CSS with a normal row direction.

Warning

Keep meaning in the source

Do not use order, row-reverse, or column-reverse to simulate a new reading sequence. If a different sequence is genuinely meaningful, place the corresponding HTML elements in that sequence instead.

Restore and compare the sequence

Restore a matching layout

For this Resource library, preserve the established document order: HTML reference, CSS layout notes, then Accessibility checklist.

Delete any temporary reverse-direction declaration and any order override. A normal row lets the visual sequence match the document sequence. Wrapping and gap adapt the layout without changing its meaning.

Container direction to keep

Use this normal container arrangement after removing the temporary visual-reordering rules. Keep your existing card sizing and readable text rules.

css
.resources {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 1rem;
}
Expected output
At a wide viewport, the visual row reads left to right: HTML reference, CSS layout notes, Accessibility checklist.

At narrower widths, cards may wrap onto additional lines, but their document and Tab sequence remains Read HTML notes, Read CSS notes, Read accessibility notes.

Expected browser result after saving the CSS and reloading the local page.

Tip

Check in your browser

Save both local files and refresh the browser. At a width where the three cards share a row, compare the visual sequence with the HTML sequence.

Then reload the page and press Tab. Leave the native focus indicator visible. It should move through the card links in the same meaningful sequence, even if wrapping creates additional lines.

Practice: repair the sequence

Arrange the source sequence

A preview displays these cards from left to right: Accessibility checklist, CSS layout notes, HTML reference. Arrange the article headings in the intended document order that repairs this mismatch.

  1. Accessibility checklist
  2. CSS layout notes
  3. HTML reference

Step 9 of 9

Build and Check the Flexible Resource Library

Build and locally verify a wrapping Flexbox resource library with readable content and a meaningful keyboard sequence.

Create the final local page

1. Finish the page structure

Replace the temporary comparison rules with the complete final local page. Keep index.html and styles.css in the same folder. The articles remain in meaningful document order, and each link points to a destination in this file.

Final Resource library markup

index.html

html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Resource library</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <main>
    <h1>Resource library</h1>

    <div class="resources">
      <article class="resource-card resource-card--featured">
        <h2 class="resource-title">HTML reference</h2>
        <p>Review the elements that give page content meaning.</p>
        <a href="#html-notes">Read HTML notes</a>
      </article>

      <article class="resource-card">
        <h2 class="resource-title">CSS layout notes</h2>
        <p>Use layout rules to arrange content clearly.</p>
        <a href="#css-notes">Read CSS notes</a>
      </article>

      <article class="resource-card">
        <h2 class="resource-title">Accessibility checklist</h2>
        <p>Check that content remains usable for everyone.</p>
        <a href="#accessibility-notes">Read accessibility notes</a>
      </article>
    </div>

    <section id="html-notes">
      <h2>HTML reference</h2>
      <p>Semantic elements describe the purpose of content.</p>
    </section>

    <section id="css-notes">
      <h2>CSS layout notes</h2>
      <p>Layout rules control how content uses available space.</p>
    </section>

    <section id="accessibility-notes">
      <h2>Accessibility checklist</h2>
      <p>Meaningful order supports reading and keyboard navigation.</p>
    </section>
  </main>
</body>
</html>
Expected output
Resource library

Three resource cards appear before the destination sections:
HTML reference
CSS layout notes
Accessibility checklist

Each card contains its matching link. Selecting a link moves to its matching section lower on the page.

After you also save the stylesheet from the next screen, exact line breaks and card rows vary with the browser width.

Compare the intended result

2. Apply the flexible card rules

This stylesheet makes .resources a wrapping flex container with a 1rem gutter. Every card starts from a 12rem preferred size, can grow and shrink, and may shrink below its automatic minimum when needed. The featured card receives a larger share of extra space on a line, while normal wrapping keeps text visible.

Final Flexbox stylesheet

styles.css

css
body {
  font-family: system-ui, sans-serif;
  margin: 0;
  padding: 2rem;
}

.resources {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 1rem;
  border: 0.125rem solid #94a3b8;
  padding: 1rem;
}

.resource-card {
  flex: 1 1 12rem;
  min-width: 0;
  border: 0.125rem solid #cbd5e1;
  padding: 1rem;
}

.resource-card--featured {
  flex: 2 1 12rem;
}

.resource-title {
  margin-top: 0;
  white-space: normal;
}
Expected output
Wide browser: the three cards can share one horizontal row with 1rem gutters. The featured HTML reference card receives a larger share of positive free space.

Narrower browser: cards move onto additional flex lines as space runs out. Card headings and text remain fully visible and can wrap normally.

Tab navigation follows the links in HTML reference, CSS layout notes, Accessibility checklist order.

The precise number of cards per line and final card widths depend on the available browser width. No text should be clipped.

Compare wide and narrow results

A wide one-row card layout and a narrower wrapped card layout, both preserving the original card sequence and fully visible content.

Extra width is shared within a flex line. As width decreases, cards wrap in document order rather than hiding their content.

Apply the final layout rules

Complete the final CSS essentials

Complete the essential shared rules for the final layout.

css
.resources {
  display: ;
  flex-direction: ;
  flex-wrap: ;
  gap: ;
}

.resource-card {
  flex:  1 12rem;
  min-width: ;
}

Verify in your browser

Run and report the local check

Save both files, then open or refresh index.html in your browser. Compare a wide window with a narrower window until cards need another line. Confirm that no essential heading or paragraph text disappears. Then reload the page, focus the page, and use Tab through the three card links. Report the visual behavior and the focus sequence you observed.

Write at least 100 characters (0/100).

Finish

Summary

Flexbox recap

  • .resources is the flex container: it creates a wrapping row and provides 1rem gutters between flex items.
  • Each .resource-card uses flex: 1 1 12rem as its flexible sizing policy; the featured override only changes its share of extra space.
  • min-width: 0 permits a constrained flex item to shrink, while white-space: normal keeps essential text readable rather than clipped.
  • The HTML remains ordered as HTML reference, CSS layout notes, Accessibility checklist, so its reading and native Tab sequence stay meaningful.

Tutorial complete

Congratulations! You completed: Arrange Content with Flexbox

You built and checked a flexible Resource library that grows, wraps, keeps content visible, and preserves a meaningful document and keyboard order.

Download the App now to have access to + 5000 free courses, exercises, certificates and lots of content without paying anything!

  • 100% free online courses from start to finish

    Thousands of online courses in video, ebooks and audiobooks.

  • More than 60 thousand free exercises

    To test your knowledge during online courses

  • Valid free Digital Certificate with QR Code

    Generated directly from your cell phone's photo gallery and sent to your email

Cursa app on the ebook screen, the video course screen and the course exercises screen, plus the course completion certificate