Coding Lesson

← All lessons

CSS: Appearance

Spacing and cards

Lesson 9 of 18 · About 20 minutes

Your goal: Create a readable content card.

Learn

A card is a block of related content with intentional spacing. Padding separates its words from its edge; margin separates that edge from neighbouring content. You will change one kind of space at a time so you can see which area each property controls.

Walkthrough: why the code works

  1. Find the card and its existing rules

    The starter uses article class="card" for the heading and introduction. Its .card rule supplies a pale-blue background. The * selector applies box-sizing: border-box to every element in this example, so a specified width includes its padding and border. Keep that rule.

  2. Make the edge visible

    Add a border to the existing .card rule. A border needs a visible style such as solid as well as a width and colour. Run the code before adding more spacing, so you can identify the edge that padding and margin refer to.

    .card {
      background: #eff6ff;
      border: 1px solid #94a3b8;
      padding: 24px;
      margin: 20px;
    }
  3. Compare inside and outside space

    With the complete rule, the 24px padding sits between the border and the content area. The 20px margin sits outside the border. The card’s background covers its padding, but the margin is transparent. Change only padding to 8px and run again, then restore it and change only margin to 40px.

  4. Account for default text spacing

    Browsers give headings and paragraphs their own margins. If you want a clearer experiment, set .card h1 { margin-top: 0; } and .card p { margin-bottom: 0; }. The remaining space at the top and bottom comes from the card’s padding. Do not assume every visible gap belongs to the card itself.

  5. Keep the card flexible

    The starter has no fixed width, so the block fits the available space. Avoid adding width: 100% alongside horizontal margins without checking for overflow: margins remain outside the box even with border-box. Add border-radius: 8px if you want rounded corners, then test a narrow preview. For a fuller width calculation, read the CSS Box Model article in the blog.

Expected results

  • A thin border surrounds the card and the text has space inside it.

  • Reducing padding moves the content closer to the border; changing margin changes the outside space.

  • The card remains readable when the preview becomes narrower, with text wrapping naturally.

Common mistakes and how to fix them

  • The border is invisible: Use a complete declaration such as border: 1px solid #94a3b8. A colour and width alone do not supply a visible border style.

  • Margin changes the wrong space: Use padding for the space inside the card. Margin belongs outside its border.

  • A fixed width overflows: Remove the unnecessary fixed width and inspect horizontal margins. border-box includes padding and borders, not margins.

View starter code
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My hobby page</title>
    <style>
      * {
        box-sizing: border-box;
      }
      .card {
        background: #eff6ff;
      }
    </style>
  </head>

  <body>
    <article class="card">
      <h1>My hobby</h1>
      <p>A short introduction.</p>
    </article>
  </body>
</html>

Your challenge

Add padding, a border and a margin to the card. Compare the spaces inside and outside.

Keep developing your hobby page: Download HTML before leaving the sandbox, then choose Open HTML file when returning to your project. Load saved draft restores only this lesson’s draft in this browser.

Practise in the sandbox
Show a hint

Try padding: 24px; margin: 20px; border: 1px solid #94a3b8;.

Show a sample answer

Try the challenge first. This is one possible answer or a snippet to adapt.

.card {
  padding: 24px;
  margin: 20px;
  border: 1px solid #94a3b8;
  border-radius: 8px;
}

Check your work

Text does not touch the border and the card has space around it.

Completion is your own check, not an automatic grade.