🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Web & Software Development › HTML, CSS, and JavaScript for Web Development › Lesson 5
TEC-WEB46-02 · Lesson 5 · 16 clock hours · lab

CSS Layouts: Flexbox and CSS Grid

Objective. Students build multi-column and multi-row page layouts using CSS Flexbox and CSS Grid, correctly applying axis, alignment, sizing, and spanning properties.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

CSS Layouts: Flexbox and CSS Grid

Modern web pages rarely consist of a single column of text. Designers arrange content into cards, sidebars, navigation bars, and multi-row grids. CSS provides two purpose-built layout systems for this work: Flexbox and CSS Grid. Each solves a different class of problem, and professional developers choose between them deliberately.

How Flexbox Works

Flexbox (the Flexible Box Layout Module) arranges items along one axis at a time — either a row or a column. You apply it by setting `display: flex` on a container element. The direct children of that container become flex items and are placed along the main axis. The perpendicular direction is the cross axis.

Key container properties:

Key item properties:

For the card gallery lab, you will set `display: flex` and `flex-wrap: wrap` on the gallery container, give each card a `flex` basis (e.g., `calc(33% - 2rem)`), and use `align-items: stretch` so all cards in a row reach the same height. Centering content inside each card is a nested Flexbox pattern: set `display: flex; flex-direction: column; justify-content: center` on the card itself.

How CSS Grid Works

CSS Grid is a two-dimensional layout system. You define explicit rows and columns on a container, then place items into named or numbered cells. Apply it with `display: grid`.

Key container properties:

Key item properties:

For the page-layout lab, you will declare a three-row, two-column grid. The `grid-template-areas` value will look like this:

```css

grid-template-areas:

"header header"

"content sidebar"

"footer footer";

```

Each child element receives a matching `grid-area` name. The header and footer span both columns automatically because their name fills both cells in that row.

Choosing Between Flexbox and Grid

A useful rule of thumb: Flexbox is content-out; Grid is layout-in.

Use Flexbox when the number or size of items is unknown at design time and you want the browser to distribute space naturally — navigation links, button groups, or a card gallery where cards can wrap freely. The `flex-wrap` and `justify-content` properties handle variable counts gracefully.

Use Grid when you have a fixed, two-dimensional structure that items must conform to — a full-page shell with header, sidebar, main, and footer, or a dashboard with precisely positioned widgets. `grid-template-areas` makes the structure visible in the CSS itself, which improves maintainability.

The two systems are not mutually exclusive. A common professional pattern is Grid for the outer page shell and Flexbox for components inside grid cells.

Using DevTools to Inspect Layouts

Chrome DevTools and Firefox both include layout overlays. In Chrome, open DevTools, select the Elements panel, and look for the flex or grid badge next to a container element in the DOM tree. Clicking the badge toggles a colored overlay showing tracks, gaps, and item boundaries. Firefox's Grid Inspector panel offers similar visualization with named-area labels.

Use these tools during the lab to verify that items are placed in the correct tracks and that wrapping occurs where you intend. If a card overflows its container, inspect the item's computed `flex-basis` and `min-width` — a `min-width` larger than the basis is the most common cause.

Workplace Safety Note

Screen-based development work involves prolonged static posture and close monitor viewing. Follow ergonomic best practices: position your monitor so the top of the screen is at or slightly below eye level, keep your wrists neutral when typing, and take a short break away from the screen at least once per hour. Proper workstation setup is a recognized occupational health practice in office and lab environments.

Key terms10

Flex containerAn element with `display: flex` applied; its direct children become flex items laid out along the main axis.
Main axisThe primary direction in which flex items are placed, set by `flex-direction`; either horizontal (row) or vertical (column).
Cross axisThe axis perpendicular to the main axis in a Flexbox layout, controlled by properties such as `align-items` and `align-self`.
fr unitA fractional unit used in CSS Grid track sizing; `1fr` represents one share of the remaining space after fixed-size tracks and gaps are accounted for.
grid-template-areasA CSS Grid property that assigns human-readable names to grid cells; items are then placed by referencing those names with `grid-area`.
flex-wrapA Flexbox container property; when set to `wrap`, items that exceed the container width flow onto a new line instead of overflowing or shrinking below their content size.
grid-column / grid-rowCSS Grid item properties that specify which grid lines an item starts and ends on, enabling an item to span multiple columns or rows.
justify-contentA property that aligns flex items or grid tracks along the main axis (Flexbox) or inline axis (Grid) of the container.
align-itemsA container property that aligns flex items along the cross axis (Flexbox) or block axis (Grid), affecting all items unless overridden by `align-self`.
gapA shorthand property (replacing the older `grid-gap`) that sets the spacing between rows and columns in both Flexbox and Grid layouts.
In class: Flexbox card-layout lab: students use Flexbox to build a product card gallery that wraps responsively, maintains equal card heights, and centers card content vertically — verified by resizing the browser window. · CSS Grid page-layout lab: students implement a full page layout (header spanning all columns, a two-column content/sidebar area, and a footer) using named grid areas defined in grid-template-areas. · Layout comparison writeup: students write a minimum 200-word explanation comparing when Flexbox is preferable to Grid and vice versa, supported by specific property-level examples from their two labs.
Materials: VS Code, Live Server, Chrome DevTools Grid and Flexbox overlays, CSS-Tricks Flexbox Guide and Grid Guide (css-tricks.com), Firefox Grid Inspector
Check: Instructor loads both lab files and resizes the viewport; card gallery must wrap without overflow at three tested widths, and Grid layout must match the provided wireframe at desktop width; pass mark both layouts functional plus comparison writeup submitted.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

CompTIA ITF+ — Software and Application Concepts
1. A developer wants a navigation bar where five links are spaced evenly across the full width of the header, with equal space between each link. Which CSS declaration on the flex container achieves this?
CompTIA ITF+ — Software and Application Concepts
2. In a CSS Grid layout, a developer writes `grid-template-areas: "header header" "main sidebar" "footer footer";`. How many columns does this grid have?
CompTIA ITF+ — Software and Application Concepts
3. A card gallery uses `display: flex` and `flex-wrap: wrap`. At a narrow viewport width, cards stack into a single column, but one card overflows outside the container boundary. What is the most likely cause?
CompTIA ITF+ — Software and Application Concepts
4. Which CSS Grid property places an item into a named region defined by `grid-template-areas`?
CompTIA ITF+ — Software and Application Concepts
5. A developer needs three equal-width columns in a CSS Grid that share all available horizontal space. Which `grid-template-columns` value is correct?
CompTIA ITF+ — Software and Application Concepts
6. When is CSS Flexbox generally preferable to CSS Grid?
CompTIA ITF+ — Software and Application Concepts
7. A developer sets `align-items: center` on a flex container with `flex-direction: row`. Which axis is affected?
CompTIA ITF+ — Software and Application Concepts
8. A developer inspects a CSS Grid layout in Chrome DevTools and sees a badge labeled 'grid' next to a `<div>` in the Elements panel. What happens when the developer clicks that badge?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.