Skip to main content
← HTML and CSS

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

  1. It shows three cards in a row, centred on the page.
  2. Point at a card. It lifts and its border changes colour.
  3. 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.

PropertyThe question it answers
gapHow much space goes between the children?
flex-wrapWhat happens when they do not fit?
justify-contentWhere does the spare space go across the row?
align-itemsHow 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.

1.5rem

Only the space between the cards changes. Nothing else moves.

200px

Make the cards wide enough. The row then wraps onto a second line by itself.

8px

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-contentWhat it does
flex-startAll the spare space goes on the right
centerBoth ends share the spare space equally
space-betweenAll the spare space goes between the cards
space-aroundEvery card gets its own margin of space
align-itemsWhat it does
flex-startTops line up. Cards keep their own heights
stretchEvery card grows to the height of the tallest
centerMiddles 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);
}
  • :hover is a pseudo-class. It only applies while the pointer is over the element. You used one in Geological Wonders, on a table row.
  • transform: translateY moves an element up or down. Negative numbers move it up. That is because Y is measured downwards on a screen.
  • transition goes 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.

#ffffff
#2563eb

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.

  1. 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.

  2. 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.

  3. Turn on flex

    Add a .deck rule with one line in it. Reload. The cards are in a row.

    .deck {
        display: flex;
    }
  4. Add the gap

    The cards are touching. Add gap: 1.5rem. Try 0, then 4rem, then put it back.

  5. Handle a narrow window

    Drag the window narrow. The cards squash. Add flex-wrap: wrap and try again. Now the last card moves to a new line instead.

  6. Place the row

    Add justify-content: center and align-items: flex-start. Try the other values from the tables above before you decide.

  7. Add the hover

    Write the .card:hover rule with transform, box-shadow, and border-color. Then add transition to the .card rule 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: flex first, 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: 0 looks 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.