Lesson 5 of 7
Cards in a Row
One line of CSS puts three cards side by side. This is flexbox. Flexbox is how you arrange a page.
What you will learn
- display: flex
- gap
- flex-wrap
- justify-content
- align-items
- Parent and child
- :hover
- transition
- transform
Before you start
Trading Card. This lesson uses the same card three times. The new part is the box around them.
What this page does
- It shows three cards in a row, centred on the page.
- Point at a card. It lifts and its border changes colour.
- Make the window narrow. The third card drops onto a second line on its own.
Flexbox in one sentence
Put display: flex on a box. The children of that box then line up in a row. Without it they stack down the page.
.deck {
display: flex;
gap: 1.5rem;
flex-wrap: wrap;
justify-content: center;
align-items: flex-start;
}Everything else in flexbox is a detail of that one idea.
| Property | The question it answers |
|---|---|
gap | How much space goes between the children? |
flex-wrap | What happens when they do not fit? |
justify-content | Where does the spare space go across the row? |
align-items | How do they line up down the row? |
In index.html, the <div class="deck"> is the parent. The three <div class="card"> blocks are its children. Comment out display: flex and they stack again, one per line. That is what a <div> does on its own.
The deck and the cards
Make the cards wide enough. Then `flex-wrap` sends the last card to a new line on its own. You do not write any extra code.
Only the space between the cards changes. Nothing else moves.
Make the cards wide enough. The row then wraps onto a second line by itself.
Point at a card after you move this.
Values worth trying
These two properties take words, not numbers. That is why they have no slider. Change one in index.css, look at the page, then change it back.
justify-content | What it does |
|---|---|
flex-start | All the spare space goes on the right |
center | Both ends share the spare space equally |
space-between | All the spare space goes between the cards |
space-around | Every card gets its own margin of space |
align-items | What it does |
|---|---|
flex-start | Tops line up. Cards keep their own heights |
stretch | Every card grows to the height of the tallest |
center | Middles line up |
Reading that list teaches nothing. Watching the page move teaches it fast. First make one card's quote much longer. The cards then have different heights, so align-items has something to do.
One class, three cards
All three cards say class="card". So one .card rule styles all of them. Change the rule and all three cards change together. That is the reason to use classes. It is also why the one card in the last lesson was worth building properly.
Copy a whole card block to make a fourth card. It appears fully styled, because it has the same class.
Hover, and moving things
.card {
transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
}
.card:hover {
transform: translateY(-8px);
}:hoveris a pseudo-class. It only applies while the pointer is over the element. You used one in Geological Wonders, on a table row.transform: translateYmoves an element up or down. Negative numbers move it up. That is because Y is measured downwards on a screen.transitiongoes on the normal rule, not the hover rule. It tells the browser to take 0.2 seconds when the property changes. Without it, the change jumps.
The card and its hover
Point at a card while you change these.
Point at a card to see this one.
Build it step by step
Start with your finished card from lesson 4. This lesson adds one wrapper and one rule.
Copy the card twice
In
index.html, copy the whole<div class="card">block so you have three cards. Change the names, the emoji, and the quotes. Reload. They stack down the page.Wrap them
Put one
<div class="deck">around all three cards. Reload. Nothing changes, because a div on its own does nothing. That is worth seeing.Turn on flex
Add a
.deckrule with one line in it. Reload. The cards are in a row..deck { display: flex; }Add the gap
The cards are touching. Add
gap: 1.5rem. Try0, then4rem, then put it back.Handle a narrow window
Drag the window narrow. The cards squash. Add
flex-wrap: wrapand try again. Now the last card moves to a new line instead.Place the row
Add
justify-content: centerandalign-items: flex-start. Try the other values from the tables above before you decide.Add the hover
Write the
.card:hoverrule withtransform,box-shadow, andborder-color. Then addtransitionto the.cardrule and point at a card again.
Try these changes
Make these changes in the files beside this guide. Each one only uses what this lesson has already covered. Tick each one when it works.
Check yourself
Three quick questions on this lesson. Get them all right to finish the lesson and earn a pack.
Loading your answers
Teacher's versionHow to run this lesson, what to check, and any answers. Students do not need this.
How to teach it
- Comment out
display: flexfirst, so they see the stack. Then put it back. One line changes the whole layout. Do it twice. - Say the parent and child rule out loud, and keep saying it. The container decides, and the children show up. This idea carries into grid. It carries into every layout framework they will ever meet.
- Ask them to predict what
gap: 0looks like before you set it. Predictions make the demonstration stick. - Have them copy a card to make a fourth and a fifth. The new cards appear already styled. That is the moment a class stops being a rule and starts being useful.
- Save the hover effect for the last ten minutes. It is the reward. They will happily tune the lift and the shadow on their own.