Master modern CSS layout systems — arrange elements in rows, columns, and grids with ease.
Flexbox Basics
Flex Properties
CSS Grid
Responsive
Quiz
Flexbox Basics
Flexbox is like arranging books on a shelf. You choose the shelf direction (left-to-right or top-to-bottom), then decide how to space the books out — push them to the left, center them, or spread them evenly across the shelf.
Flexbox works with two concepts:
Flex container — the parent element with display: flex
Flex items — the direct children inside the container
HTML — Flexbox: justify-content & align-items
Property
What It Does
Common Values
justify-content
Horizontal alignment
flex-start, center, flex-end, space-between, space-around, space-evenly
Beyond the container, each flex item can control its own behavior with properties like flex-grow, flex-shrink, and flex-wrap.
HTML — flex-grow, flex-wrap, and flex-direction
The shorthand flex: 1 is the same as flex-grow: 1; flex-shrink: 1; flex-basis: 0. It tells an item to take up an equal share of available space. It's the most commonly used flex shorthand!
CSS Grid
If Flexbox is a bookshelf (one-dimensional, items in a row or column), CSS Grid is a spreadsheet (two-dimensional, items in rows AND columns at the same time). Use Flexbox for simple rows/columns, and Grid for complex layouts.
HTML — CSS Grid Layout
Grid Property
What It Does
Example
grid-template-columns
Defines column sizes
1fr 1fr 1fr (3 equal)
grid-template-rows
Defines row sizes
60px 1fr 50px
grid-column
Span columns
1 / 3 (spans 2 cols)
gap
Space between cells
12px
fr
Fraction of free space
2fr = twice as wide
Responsive Design with Media Queries
Media queries are like a wardrobe for different weather. You wear shorts in summer and a coat in winter. Similarly, your website wears different styles depending on the screen size — big layouts on desktops, stacked layouts on phones.
HTML — Responsive Media Queries
Always design mobile-first! Start with styles for small screens, then use @media (min-width: ...) to add layout for larger screens. This approach makes responsive design much easier.
🎯 Challenge!
Add a 4th card to the grid
Change the large-screen layout to 4 columns: grid-template-columns: 1fr 1fr 1fr 1fr
Add a breakpoint at 500px for 2 columns
📝 Chapter 3 Quiz
1. What CSS property activates Flexbox on a container?
flex: 1
position: flex
display: flex
flexbox: true
2. Which property distributes items with equal space BETWEEN them?
align-items: space-between
justify-content: space-between
gap: space-between
flex-wrap: space-between
3. How do you create 3 equal columns with CSS Grid?
grid-template-columns: 1fr 1fr 1fr
grid-columns: 3
columns: 3 equal
display: grid-3
4. Which syntax is correct for a media query?
@screen (max-width: 768px) { }
@responsive (768px) { }
@breakpoint 768px { }
@media (max-width: 768px) { }
5. What is the key difference between Flexbox and Grid?
Flexbox is newer than Grid
Grid cannot center items
Flexbox is 1D (row or column), Grid is 2D (rows and columns)