Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Box Model

In this lesson, we'll look at how much space elements take up on a page and what separates them from each other. The key idea is simple: every element on a page is a rectangular box, even when you can't see its edges.

For example, when you give a heading a background color, the whole row of the page gets colored, not just the area behind the text. That's because the background fills the element's whole box, and most elements, such as headings, paragraphs and divs, are as wide as the element they are in (their parent) by default. Some elements, such as strong or a, are only as wide as their content. We'll see why in the lesson Display and Position.

Each element's box has four parts, from the inside out:

The box model: the content in the middle, around it the padding, then the border and on the outside the margin
  • Content: the content of the element itself, such as the text of a paragraph or an image.
  • Padding: the space between the content and the border.
  • Border: a line around the content and the padding. It can also be invisible.
  • Margin: the space outside the border, which separates the element from the elements around it.

Imagine a framed picture on a wall: the content is the picture itself, the padding is the mat (the strip of card between the picture and the frame), the border is the frame, and the margin is the gap between this frame and the next picture on the wall.

In this lesson, we'll learn to set each of these parts with CSS. The cat page already has a short style.css with the colors and fonts you know from the previous lessons. To make the boxes visible, we'll give every element a red border for now. You'll see that the boxes of the headings span the whole width even though their text is short, while the box of the strong element is only as wide as its text.

Tip: You can look at the box of any element on any website (including the preview here) right in your browser. Right-click the element and choose Inspect. The developer tools open, and when you hover over an element in them, the browser highlights its content, padding, border and margin on the page in different colors.

Instructions

  1. 1

    At the end of style.css, create a rule for all elements (the selector *) with the declaration border: 1px solid red. We'll explain what exactly 1px solid red means later in this lesson.

Back

1/12

  1. 1. Box Model
  2. 2. Width and Height
  3. 3. Units
  4. 4. Borders
  5. 5. Rounded Corners
  6. 6. Padding
  7. 7. Box Sizing
  8. 8. Margins
  9. 9. Several Values at Once
  10. 10. Centering
  11. 11. Overflow
  12. 12. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.