Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

The box model

2m:50s

The general box model is a layered model consisting of a content box and optional surrounding margin, border and padding areas. This model can be visualised with the following diagram:

Diagram illustrating content, padding, border and margin

Every browser’s Devtools provides such a visualisation for every element it renders on the page, give it a try on your own preferred browser with the example code.

Firefox Devtools showing the box model visualisation of the heading element
Chrome Devtools showing the box model visualisation of the heading element
Safari Devtools showing the box model visualisation of the heading element

You will notice that each of the Devtools’ box model diagrams have values on the different layers. These are the sizing values of each of the respective layers. And when you hover over any element on the page, you can see the margins and paddings applied highlighted in different colours, if they exist. Formally, the specification describes this model like so:

Each box has a content area (e.g., text, an image, etc.) and optional surrounding padding, border, and margin areas.

Understanding how these layers work will help you figure out the size of your boxes, which is important when you think about laying out all your content on a page, and how things ought to fit together to form a cohesive design.

Content box

The innermost box is called the content box, and this is where the content is bounded. Content could be lines of text or an image, or even some embedded media. You can control the dimensions of this box using the width and height properties.

Padding box

Immediately after the content box comes the padding box. It is, as its name describes, a layer of padding around the content box. You can add padding on either of the four sides of the content box using the padding-top, padding-right ,padding-bottom and padding-left properties.

Border box

You can add borders around elements on your webpage, and any borders applied will come after the padding layer. You can control the style of the borders, in terms of colour and width as well as whether you’d like a solid line or dashed line. The border properties available to you are border-width, border-color and border-style for each of the four sides.

Margin box

The outermost layer in this model is the margin box, and this determines the space beyond the border. This is used for adjusting the space between the box itself and the boxes around it. Like padding and borders, you can control the space on each of the four sides using the margin-top, margin-right ,margin-bottom and margin-left properties.

The difference between margin and padding

Even though both properties introduce spacing between your content itself and anything outside the content box, they behave differently. Padding adds space within the element while margin adds space around the element.

If your element had a border around it, padding would occur within the border while margin occurs outside the border.

Highlighting a box with margin, border and padding applied

Your browser’s Devtools highlight padding and margin in different colours, which helps when debugging. In addition, having space added within the element means that if the element is a link, the padding area counts as part of the link, but not the margin area.