CSS Basics · Lesson 5 of 11

The Box Model

Every element on a page is a rectangular box. Around its content, a box has three layers:

  • Padding: space inside the box, between the content and the border. The background color fills it.
  • Border: a line around the padding.
  • Margin: space outside the border, pushing other elements away. It is always transparent.
.card {
  padding: 20px;
  border: 2px solid black;
  margin: 30px;
}

Borders

border takes three values in one go: thickness, style and color. Common styles are solid, dashed and dotted.

Round the corners of a box with border-radius:

.card {
  border-radius: 12px;
}

One side at a time

Each layer can be set per side: margin-top, padding-left, border-bottom and so on. There is also a shorthand with two values, where the first is top and bottom and the second is left and right:

padding: 10px 24px;

Padding or margin?

If you want more room inside the colored area, use padding. If you want to move the whole box away from its neighbors, use margin.

Your Task

  1. Give .card a padding of 20px on all sides.
  2. Give it a border that is 2px thick, solid, in any color.
  3. Round its corners with a border-radius of 12px.
  4. Add a margin-bottom of 24px so the two cards do not touch.
  • The cards have 20px of padding on every side
  • The cards have a 2px solid border all the way around
  • The corners are rounded by 12px
  • The cards have a bottom margin of 24px