smartyfy.mrindigi.com

Web Development Pro | Lesson 014 | Flexbox for One-Dimensional Layout

Web Development Pro · Lesson 14 of 60

Module 2: CSS and responsive design

In this lesson

Prerequisites and learning outcomes

Complete lesson 13 first. This lesson builds on axes, alignment, wrapping, and gaps. By the end, you can build a wrapping navigation and a flexible card row without reordering meaning. Plan 10–15 minutes for the core explanation and additional time for the worked practice.

Use flexbox for a line of related items

The workshop navigation should place links beside one another when space allows and wrap them when it does not. A row of badges or a card's title-and-action pair has a similar need. Flexbox organizes items primarily along one dimension: a main axis, with alignment across a cross axis. It is especially useful when items have different sizes but should share or distribute available space.

Setting display: flex on a parent makes its direct children flex items. Deeper descendants do not automatically become part of that same flex layout. This boundary matters when a navigation contains a list: if the links are inside li elements, flex the list to arrange those list items, rather than assuming the outer nav will arrange every nested link.

Identify the axes first

Flex-direction: row makes the main axis follow the inline direction; column makes it follow the block direction. For this English horizontal-writing example, row usually means left to right and column top to bottom. The conceptual axes are more general than those physical directions. Justify-content works along the main axis, and align-items works across it.

.site-nav ul {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1rem;
  list-style: none;
  padding: 0;
  margin: 0;
}
.site-nav a { display: block; padding: 0.6rem; }
.card-row { display: flex; flex-wrap: wrap; gap: 1rem; }
.card { flex: 1 1 16rem; min-width: 0; padding: 1rem; border: 1px solid #28536b; }

Gap creates space between items and, with wrapping, between lines. It does not add the same spacing around the container's outer edge; use padding for that. Keeping internal gaps and external padding separate makes the layout easier to reason about than assigning different margins to first and last items.

Understand grow, shrink, and basis

The card shorthand flex: 1 1 16rem means a grow factor of one, a shrink factor of one, and a starting basis of sixteen rem. The layout algorithm distributes space using these values and other size constraints. The basis is not a promise that every card will remain exactly sixteen rem wide. Available space, wrapping, content, and minimum sizes affect the final result.

Equal grow factors divide positive free space equally, but they do not always produce equal final widths when starting bases differ. Shrinking also depends on scaled shrink factors rather than simply subtracting the same amount from every item. For a beginner-friendly equal-card row, use matching bases and factors, then inspect real content rather than relying on a slogan about equal fractions.

Flex items have automatic minimum-size behavior that can prevent them from shrinking around long contents. Min-width: 0 on a horizontal flex item allows it to shrink below its content-based minimum when appropriate. That does not make unbreakable text readable by itself; add suitable wrapping or overflow treatment for the content as well.

Let items wrap when the task permits

Without flex-wrap: wrap, the default is a single flex line. Items may shrink, overflow, or become cramped depending on their constraints. Navigation usually benefits from wrapping instead of squeezing labels into unreadable fragments. A row of cards may also wrap naturally, while a toolbar could need a different deliberate responsive strategy.

Wrapped flex lines distribute their own available space. Cards on the last line may therefore be wider than cards above. This is not a bug; it follows flexbox's line-based model. If you need columns to align consistently across multiple rows, grid may be the clearer choice. The next lesson compares that structure directly.

Preserve source order and focus order

Flexbox includes order and reversed directions that change visual placement. Those changes do not necessarily change the DOM reading order or keyboard sequence. Use them cautiously. A visual “first” link that is reached third by keyboard can be confusing, particularly when the layout suggests a process or sequence.

For this site, keep the HTML in the desired reading order and let wrapping adapt it. Do not rearrange important instructions solely to create a more symmetrical design. A small visual inconvenience is easier to solve than two competing navigation orders. When reviewing, compare pointer use, keyboard movement, and the source sequence.

Diagnose with a compact checklist

If a row behaves unexpectedly, ask whether display: flex is on the right parent, whether the items are direct children, which axis is active, whether wrapping is enabled, and whether a minimum size is resisting shrinkage. These questions isolate distinct causes. Changing justify-content repeatedly will not fix a long word that forces an item wider than its container.

The supplied example includes long card text to make shrink behavior worth inspecting. Static checks confirm the rules and structure, but only a rendering browser can establish the final wrapping at a particular viewport and font size. Record narrow and wide observations when you run it locally, including any horizontal scrolling.

Worked practice

Worked exercise 1: Choose the correct parent

Task: A nav contains ul, which contains three li items. You want those items in a wrapping row. Worked answer: put display: flex and flex-wrap: wrap on ul. The li elements are its direct children. Flexing nav would primarily arrange the ul as one item.

Worked exercise 2: Explain a wide last card

Task: Three equal flex cards wrap into two cards above and one below; the bottom card stretches. Worked answer: each flex line distributes free space independently. Use grid if aligned columns across rows are required, or accept the wider final card if it fits the design.

Worked exercise 3: Repair a source-order mismatch

Task: CSS order makes Schedule appear first, but Tab reaches Home first. Worked answer: put the links in the desired logical order in HTML and remove the visual reordering. Then test both visual scanning and keyboard navigation to confirm they tell the same story.

Knowledge check

Question 1

Which elements become flex items? A) All descendants. B) Direct children of the flex container. C) Only direct children with display: block. D) Only children with a non-auto width.

Check question 1: answer and explanation

Answer: B. Flex layout applies to the container's direct children.

Question 2

What does justify-content control? A) Main-axis distribution. B) Text justification inside paragraphs. C) Cross-axis alignment of individual items. D) The order of items along the main axis.

Check question 2: answer and explanation

Answer: A. Its physical direction depends on the chosen main axis.

Question 3

What does the third value in flex: 1 1 16rem represent? A) A guaranteed final width. B) A gap. C) The flex basis. D) The item’s maximum allowed width.

Check question 3: answer and explanation

Answer: C. Basis is an input to layout, not an unconditional final measurement.

Question 4

Why might grid suit a multirow card layout better? A) Flex items cannot wrap onto more than one line. B) Grid aligns tracks across rows and columns. C) Grid changes keyboard order automatically. D) Grid eliminates all overflow.

Check question 4: answer and explanation

Answer: B. Shared two-dimensional tracks fit that alignment requirement.

Question 5

Does CSS order reliably change keyboard navigation to match visual order? A) Yes, for every browser. B) Yes, if values are positive. C) Only when wrapping. D) No.

Check question 5: answer and explanation

Answer: D. Visual reordering does not replace a meaningful source and focus order.

Sources and further reading