Lesson 4 of 7
Trading Card
Give elements a class. Then use the box model to turn nine plain elements into a card.
What you will learn
- class
- Class selectors
- <div> and <span>
- The box model
- padding vs margin
- border-radius
- box-shadow
- margin: auto
Before you start
Geological Wonders. You have linked a stylesheet and used element selectors. Here you give your own elements a name. Then you style them by that name.
What this page does
- It shows one trading card in the middle of the page.
- The HTML is nine elements stacked one under another. On their own they would look like a plain list of text.
- Every one of them has a
class. The stylesheet finds them by that class. It turns the stack into a card.
A class is a label
<div class="card">.card {
width: 300px;
padding: 1.25rem;
}class is a name you put on an element. In the stylesheet, the dot in front of .card means any element with that class. A selector with no dot matches a tag name instead.
| Selector | Matches |
|---|---|
body | An element, found by its tag name |
.card | Every element with class="card" |
.stats .value | A value element inside a stats element |
You can put the same class on as many elements as you like. That is the point of a class. Lesson 5 needs exactly that: three cards, one rule.
<div> and <span> are two tags with no meaning of their own. A <div> groups blocks and starts a new line. A <span> wraps a few words and stays on the same line. Neither one changes how the page looks until your CSS says so. They exist so you have something to put a class on.
The box model
Every element on a page is a box. Every box has four layers.
| Layer | Where it is | What it does |
|---|---|---|
| content | The middle | The text or the picture itself |
| padding | Inside the border | Space around the content. Makes the box bigger |
| border | Around the padding | The line you can see |
| margin | Outside the border | Space around the box. Moves the box and pushes its neighbours away |
The four numbers on .card
These are the real values in `index.css`. Move one. The file changes along with the page.
Space inside the card. Watch the card get bigger, not just emptier.
Set it to 0 for a sharp card, then try 3.
The card stays centred at every width. That happens because of margin: auto.
margin: 3rem auto;Four values in margin go clockwise from the top: top, right, bottom, left. Two values mean top and bottom, then left and right. auto on the sides means share the leftover space equally. That centres the box. It only works when the box has a width. This card has one.
The two properties that make it look real
border-radius: 1rem;
box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);border-radiusrounds the corners.0gives sharp corners. A big number makes a pill.box-shadowtakes four values: how far right, how far down, how blurred, and what colour. Put the shadow slightly below the element. That makes the element look lifted off the page.
Make it your card
These are every colour on the card, straight out of `index.css`. A hex code like `#2563eb` is three pairs of characters. They set red, green, and blue.
Build it step by step
Build the HTML first. Do not add a stylesheet yet. Add the CSS afterwards, one property at a time.
Make the page and the outer box
Start a new
index.htmlwith!and Tab. Inside<body>, add one<div>withclass="card".Fill the card
Inside the div, add the rarity, the name, the art box, the quote, and a list of stats. Give each one a class. Reload the page. You get a plain stack of text. That is correct for now.
<div class="card"> <p class="rarity">Rare</p> <h1 class="name">Cinder Fox</h1> <div class="art">🦊</div> <p class="quote">Sleeps in warm ash. Wakes up grumpy.</p> </div>Link the stylesheet
Make
index.cssin the same folder. Add the<link>line inside<head>. Put one rule in the file to prove it is connected. Then reload the page.<link rel="stylesheet" href="index.css">Give the card a shape
Write the
.cardrule with onlywidth,background, andborder. Reload the page. You get a box. It sits at the left edge of the page.Move it and pad it
Add
margin: 3rem auto. The card jumps to the middle. Then addpadding. The text stops touching the border.Polish
Add
border-radius, thenbox-shadow, thentext-align: center. Reload after each one. These three lines turn a box into a card.Style the inside
Write the rules for
.rarity,.name,.art,.quote, and.stats. Do one rule at a time. Reload after each one.
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
- Open the page with the
<link>line deleted. Then paste the line back in front of them. The before and after is the argument for CSS. It takes five seconds. - Teach padding and margin with the sliders, side by side. It takes one minute. Padding makes the box bigger. Margin makes the box move. Say it that way and have them say it back.
- Draw the four layers on the board as boxes inside boxes before you show any code.
box-shadowandborder-radiusare the two properties that suddenly make a student's page look real. Spend a minute on them. The motivation is worth more than the minute.- Do not introduce flexbox here. Someone will ask how to put two cards side by side. Tell them that is the next lesson, and it is one line long.