Professional Business Website · Phase 3 · Lesson 2 of 3
Layouts with Flexbox and Grid
Arrange the header, navigation, service cards and gallery with flexbox and CSS grid, so layouts adapt to the space available instead of using fixed widths.
What you need
A computer (Windows, macOS or Linux) with Visual Studio Code (free) and a current web browser such as Chrome or Firefox. A smartphone is used for testing.
In this lesson you will
- Use flexbox for one-dimensional layouts
- Use grid for card and gallery layouts
Key concepts
flexboxgridgapauto-fillminmaxfluid width
The lesson
Flexbox arranges items in a row or column, ideal for the header and navigation. CSS grid arranges items in rows and columns, ideal for service cards and the photo gallery.
A grid with repeat(auto-fill, minmax(250px, 1fr)) creates as many columns as fit: one on a phone, two on a tablet, three or four on a desktop, without any media query.
Avoid fixed pixel widths on containers; use max-width and percentages so nothing overflows small screens.
Step by step
- Lay out the header and navigation with flexbox.
- Build the services section as a grid of cards.
- Build the gallery as a responsive image grid.
- Resize the browser from 320 to 1400 pixels and fix any overflow.
Practice activity
Lay out the header, service cards and gallery with flexbox and grid, and record screenshots at phone, tablet and desktop widths.
Expected result: Layouts that adapt from one to several columns with no horizontal scrolling.
Common mistakes
- Setting width: 1200px on a container, causing horizontal scrolling on phones
- Using flexbox for a full gallery and fighting uneven rows
Check your understanding
-
When is grid a better choice than flexbox?
Show answer
For two-dimensional layouts such as card grids and galleries.
-
What does minmax(250px, 1fr) do in a grid?
Show answer
Makes columns at least 250 pixels wide and lets them share the remaining space.
Further learning
Projects are instructor-guided: an instructor sets milestones, checks your work and gives feedback, online or in person. Self-study of the public lessons does not lead to a certificate. Enrolment and fees follow the Academy's existing policies — contact the Academy.