Lesson 8 of 14 · 9 min

The box model

The browser draws every element as a rectangular box. Around the content are three layers:

+------------------- margin -------------------+
|  +--------------- border ----------------+   |
|  |  +----------- padding -------------+  |   |
|  |  |            content              |  |   |
|  |  +---------------------------------+  |   |
|  +---------------------------------------+   |
+----------------------------------------------+
  • padding: space inside the border, between the border and the content.
  • border: a line around the padding.
  • margin: space outside the border, pushing other elements away.
.notice {
  padding: 12px;                 /* all four sides */
  border: 1px solid #cccccc;     /* width, style, colour */
  margin: 0 0 20px 0;            /* top, right, bottom, left */
  border-radius: 6px;            /* rounded corners */
}

You can set one side at a time: padding-left, margin-top, border-bottom, and so on. Border styles include solid, dashed and dotted.

Your browser's developer tools (right-click, Inspect) show the box model of any element, which is the quickest way to understand spacing on real sites.

Your task

Style the .card class in style.css so each card has:

  • 16px of padding,
  • a 2px solid crimson border,
  • 24px of space below it (margin-bottom),
  • corners rounded by 8px.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Cards</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <div class="card">
    <h2>Maize</h2>
    <p>Plant after the first good rains.</p>
  </div>
  <div class="card">
    <h2>Sorghum</h2>
    <p>Handles dry spells well.</p>
  </div>
</body>
</html>

Your page

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…