Skip to main content
← HTML and CSS

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

  1. It shows one trading card in the middle of the page.
  2. The HTML is nine elements stacked one under another. On their own they would look like a plain list of text.
  3. 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

index.html
<div class="card">
index.css
.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.

SelectorMatches
bodyAn element, found by its tag name
.cardEvery element with class="card"
.stats .valueA 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.

LayerWhere it isWhat it does
contentThe middleThe text or the picture itself
paddingInside the borderSpace around the content. Makes the box bigger
borderAround the paddingThe line you can see
marginOutside the borderSpace 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.

1.25rem

Space inside the card. Watch the card get bigger, not just emptier.

6px
1rem

Set it to 0 for a sharp card, then try 3.

300px

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-radius rounds the corners. 0 gives sharp corners. A big number makes a pill.
  • box-shadow takes 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.

#f4f6f9
#ffffff
#2563eb
#dbeafe

Build it step by step

Build the HTML first. Do not add a stylesheet yet. Add the CSS afterwards, one property at a time.

  1. Make the page and the outer box

    Start a new index.html with ! and Tab. Inside <body>, add one <div> with class="card".

  2. 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>
  3. Link the stylesheet

    Make index.css in 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">
  4. Give the card a shape

    Write the .card rule with only width, background, and border. Reload the page. You get a box. It sits at the left edge of the page.

  5. Move it and pad it

    Add margin: 3rem auto. The card jumps to the middle. Then add padding. The text stops touching the border.

  6. Polish

    Add border-radius, then box-shadow, then text-align: center. Reload after each one. These three lines turn a box into a card.

  7. 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-shadow and border-radius are 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.