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

Summary

In this lesson, we learned that:

The box model: the content in the middle, around it the padding, then the border and on the outside the margin
  • Every element on a page is a box with four parts: the content, the padding, the border and the margin. Like a picture on a wall with its mat, its frame and the gap to the next picture.
  • Headings, paragraphs and divs are as wide as their parent by default.
  • In a browser, right-click any element and choose Inspect to see its box.
  • By default, width and height set the size of the content.
  • Sizes can be given in pixels (px, fixed), percent (%, a share of the parent), vw and vh (1% of the width or height of the browser window) or rem (the base font size).
  • A border is written as border: width style color, for example border: 2px solid #476958. The order doesn't matter, but without a style (solid, dashed, dotted and so on) there's no border. border-top, border-bottom and the like set one side only.
  • border-radius rounds the corners, and border-radius: 50% turns a square into a circle.
  • padding is the space between the content and the border, and the element's background fills it.
  • box-sizing: border-box makes width and height include the padding and the border. Many stylesheets start with * { box-sizing: border-box; }.
  • margin is the transparent space outside the border. Browsers give some elements a default margin, for example 8 pixels around body.
  • padding and margin take one to four values: padding: 10px (all sides), padding: 10px 20px (top and bottom, left and right), margin: 0 0 10px 0 (clockwise from the top).
  • The vertical margins of neighboring elements collapse: the gap is as big as the larger one.
  • margin: 0 auto centers an element that has a width.
  • overflow sets what happens to content that doesn't fit: visible (the default), hidden, scroll or auto (scrolling only when needed).

Our cat page now has cards with borders, rounded corners and spacing in a centered section. Next comes another small project: a profile card, on which you'll use fonts and the box model on your own. Then, in the lesson Display and Position, we'll find out why some elements are as wide as the page while others are only as wide as their content, and how to place elements exactly where we want them.

Instructions

  1. 1

    Congratulations on completing the lesson on the box model! The editor contains your final code from this lesson. Try changing the borders, padding and margins and watch how the page changes.

Back

12/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.