CSS – Making Webpages Beautiful
intermediate25 XP

Borders, Padding & Margins – Spacing Everything Out

Control exactly how much space is around and inside every element — the secret to professional-looking layouts!

Borders, Padding & Margins — Spacing Perfected! šŸ“

Every web element is like a box. CSS gives you three tools to control the space around that box.

The Three Spacing Properties

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ MARGIN (space outside) │ │ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ │ │ BORDER │ │ │ │ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ │ │ │ │ PADDING │ │ │ │ │ │ ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ │ │ │ │ │ │ CONTENT │ │ │ │ │ │ │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │ │ │ │ │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │ │ │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

🟦 Padding — Space INSIDE the border

CSS
.box {
  padding: 20px;            /* same on all sides */
  padding: 10px 20px;       /* top/bottom  left/right */
  padding-top: 10px;        /* individual sides */
  padding-right: 20px;
  padding-bottom: 10px;
  padding-left: 20px;
}

🟄 Border — The line around the element

CSS
.box {
  border: 2px solid black;         /* width, style, color */
  border: 4px dashed red;          /* dashed border */
  border: 3px dotted blue;         /* dotted border */
  border-radius: 10px;             /* rounded corners! */
  border-radius: 50%;              /* makes a circle! ā­• */
}

🟩 Margin — Space OUTSIDE (pushes other elements away)

CSS
.box {
  margin: 20px;             /* same on all sides */
  margin: 10px auto;        /* auto centres horizontally! */
  margin-top: 50px;         /* push 50px from top */
}

Width & Height

CSS
.card {
  width: 300px;             /* exact pixel width */
  width: 50%;               /* 50% of parent width */
  max-width: 600px;         /* never wider than this */
  height: 200px;
}

🌟 Beautiful Card Example

HTML
<div class="game-card">
  <h2>āš”ļø Minecraft</h2>
  <p>Build anything you can imagine!</p>
  <button class="play-btn">Play Now</button>
</div>
CSS
.game-card {
  width: 280px;
  background-color: #1e1e2e;
  color: white;
  border: 2px solid #7c3aed;
  border-radius: 16px;
  padding: 24px;
  margin: 20px auto;
}

.play-btn {
  background-color: #7c3aed;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px 24px;
  font-size: 16px;
}

This creates a gorgeous purple game card! šŸŽ®

Quick check

01/3

Which property adds space INSIDE an element (between the content and border)?