smartyfy.mrindigi.com

Web Development Pro | Lesson 015 | Grid for Two-Dimensional Layout

Web Development Pro · Lesson 15 of 60

Module 2: CSS and responsive design

In this lesson

Prerequisites and learning outcomes

Complete lesson 14 first. This lesson builds on tracks, gaps, and auto-fit. By the end, you can create a resilient card grid and explain when grid or flexbox fits the task. Plan 10–15 minutes for the core explanation and additional time for the worked practice.

Give rows and columns a shared structure

The workshop directory has several cards. Flexbox can arrange them, but cards on different lines may not share column widths. Grid gives the container explicit or automatically repeated tracks in two dimensions. A track is a row or column between grid lines. Items occupy grid areas within that structure, making grid useful for card collections, page regions, and other aligned layouts.

Grid is not a replacement for semantic HTML. A list of workshops can remain a ul with li children, while CSS makes it a grid. A data table should remain a real table when row-column relationships are data. The visual layout mechanism and the content's meaning are separate decisions.

Start with explicit tracks

A simple three-column grid can use repeat(3, 1fr). The fr unit shares available free space among flexible tracks after other layout needs are considered. It is not exactly one third of the entire viewport. Container padding, gaps, and track minimums affect the space being divided.

.workshop-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
  gap: 1rem;
  list-style: none;
  padding: 0;
}
.workshop-card { min-width: 0; padding: 1rem; border: 1px solid #28536b; }
.workshop-card h2 { overflow-wrap: anywhere; }

The supplied example uses this adaptive version rather than forcing three columns at every width. Auto-fit repeats as many tracks as fit and collapses unused repeated tracks. Minmax gives each track a lower bound and a flexible upper bound. The inner min chooses the smaller of the container width and sixteen rem, so the minimum can fit even when the container is narrower than sixteen rem.

Read the sizing expression in layers

Begin at the inside: min(100%, 16rem) says the preferred minimum must not exceed the container's width. Then minmax(..., 1fr) allows the column to expand and share remaining space. Finally repeat(auto-fit, ...) creates as many such columns as the available space supports. Reading inside out turns a long expression into three understandable decisions.

A common simpler version uses minmax(16rem, 1fr). That works until the available width becomes smaller than sixteen rem, at which point the minimum itself can cause overflow. Another pattern uses an explicit media query to switch column counts. Neither strategy is universally best; choose the one whose behavior is easiest to explain and test for your content.

For explicit equal columns that must shrink, minmax(0, 1fr) can avoid an automatic content minimum forcing tracks wider than expected. The contents may still need wrapping or overflow handling. A zero track minimum is not permission to make text unusably narrow; it gives the layout flexibility that your design must use responsibly.

Keep placement understandable

Grid can place an item at particular lines or across a span. For example, grid-column: 1 / -1 spans from the first to the last explicit column line. This can make a heading or notice stretch across a grid. Do not add arbitrary coordinates before you know the content's structure; auto-placement is often sufficient for a straightforward list.

Named grid areas can make a page layout readable, but the DOM should still have a sensible reading order without CSS. As with flexbox, visual placement can differ from keyboard and assistive-technology order. A sidebar drawn above the main content on one screen should not unexpectedly interrupt the logical reading sequence because its source placement was chosen only for a desktop mockup.

Avoid dense packing when it causes later content to jump visually ahead of earlier items in a meaningful sequence. Filling every gap is less important than preserving a predictable order. For an ordered set of lessons, for example, visual packing that scrambles the apparent sequence is a poor tradeoff.

Compare grid and flexbox deliberately

Use flexbox when distributing and aligning a line of items is the main problem, such as a navigation row. Use grid when shared rows and columns are the main problem, such as a card directory. They can be nested: a grid can arrange cards, while a flex row inside each card aligns a badge and a link.

Do not memorize “grid is for pages, flex is for components” as a rule. Both can operate at either scale. Describe the relationship you need first, then choose the layout model that expresses it clearly. Simpler rules are easier to debug and less likely to break when a card title becomes longer.

Test content extremes

Try one card, two cards, and five cards. Auto-fit's treatment of unused tracks changes the way remaining items fill space. Replace a title with a long phrase and resize the viewport gradually rather than testing only two preset widths. Pay attention to the widths at which the layout changes, because awkward intermediate sizes often reveal assumptions missed by a desktop screenshot.

The local package checks the HTML list structure and stylesheet text, but the grid algorithm has not been executed in a browser here. Before treating the layout as visually verified, inspect it at narrow and wide widths and with enlarged text. Preserve any observed problem as a reproducible case with the viewport and content that triggered it.

Worked practice

Worked exercise 1: Account for gaps

Task: A 900px grid has three equal columns and two 24px gaps, with no other relevant width deductions. Worked answer: 852px remains for tracks, giving 284px per column. Dividing 900 directly by three ignores the space reserved for gaps.

Worked exercise 2: Explain a minimum overflow

Task: A 220px container uses minmax(256px, 1fr). Worked answer: the track's 256px minimum exceeds the container. Use a minimum that can shrink with the container or a suitable breakpoint. Do not hide the overflow and assume the missing content is harmless.

Worked exercise 3: Choose nested layouts

Task: Align cards across rows and align two short controls inside each card. Worked answer: use grid for the card collection and flexbox for each internal control row. Each model solves a distinct relationship without forcing one mechanism to handle everything.

Knowledge check

Question 1

What is a grid track? A) A row or column between grid lines. B) An area spanning several rows and columns. C) The gap between adjacent tracks. D) A named grid line.

Check question 1: answer and explanation

Answer: A. Tracks are the structural rows and columns used by grid layout.

Question 2

What space does fr share? A) The entire physical screen. B) The container’s whole width before gaps are deducted. C) Available free space among flexible tracks. D) The width assigned to fixed-size tracks only.

Check question 2: answer and explanation

Answer: C. Gaps and other constraints affect the space available for tracks.

Question 3

Why use min(100%, 16rem) as a minimum? A) To force every column to sixteen rem. B) To let the minimum fit a narrower container. C) To force every card to equal the viewport width. D) To change the source order when columns wrap.

Check question 3: answer and explanation

Answer: B. The smaller value prevents that minimum alone from exceeding the container width.

Question 4

What should determine grid versus flexbox? A) Which layout method allows more child elements. B) Whether the layout needs gaps between items. C) Whether its children contain images. D) The layout relationship you need.

Check question 4: answer and explanation

Answer: D. One-dimensional distribution and shared two-dimensional tracks suit different problems.

Question 5

Does a static stylesheet inspection prove a grid renders correctly? A) No, rendering still needs browser evidence. B) Yes, if braces balance. C) Yes, if all files exist. D) Yes, if cards use semantic HTML.

Check question 5: answer and explanation

Answer: A. Structural evidence cannot establish the browser's actual layout.

Sources and further reading