HTML and CSS

Lesson 6 of 10

Lesson ยท 10 min

The box model

Every element is a box

The browser draws every element as a rectangular box. A box has four layers. From the inside to the outside, they are:

  1. Content: the text or picture.
  2. Padding: space between the content and the border.
  3. Border: a line around the padding.
  4. Margin: space outside the border, which pushes other boxes away.

The background color fills the content and the padding. It does not fill the margin.

Writing the values

.note {
  padding: 16px;
  border: 1px solid gray;
  margin-bottom: 24px;
}
  • A single value, such as padding: 16px, applies to all four sides.
  • Two values, such as padding: 8px 16px, set top and bottom first, then left and right.
  • You can also set one side: padding-left, margin-top, and so on.
  • border-radius rounds the corners.

How wide is a box?

By default, width is the width of the content only. Padding and border are added to it. A box with width: 300px, padding: 20px, and a 2px border is really 344px wide.

Most people prefer that width includes everything. The declaration box-sizing: border-box does that.

Your turn

Both cards have the class card, so one rule styles both. Write your declarations in style.css.

Tasks

5 to do

  1. Task 1, not checked yet. Add space inside each card. Give .card a padding of 20px.

  2. Task 2, not checked yet. Draw a border around each card. Set border to 2px solid #334155.

  3. Task 3, not checked yet. Round the corners of the cards. Set border-radius to 12px.

  4. Task 4, not checked yet. Add space between the two cards. Set margin-bottom to 16px.

  5. Task 5, not checked yet. Make each card 320px wide. Set width to 320px and box-sizing to border-box.

Task 1 of 5

Add space inside each card. Give .card a padding of 20px.

All tasks

You can read this file, and you cannot change it. Write your code in style.css.

Preview

Your page shows here