CSS: Box Model

Learn how the CSS box model works and how to style elements effectively.

What is the Box Model?

The CSS box model describes the rectangular boxes generated for elements in the document tree. It consists of:

Example: Box Model

/* Example */
div {
    width: 200px;
    padding: 20px;
    border: 5px solid black;
    margin: 10px;
}
        

This example creates a box with content, padding, border, and margin.

Box-Sizing Property

The box-sizing property controls how the width and height of an element are calculated:

/* Example */
div {
    box-sizing: border-box;
}
        

This example ensures the total width and height include padding and border.

Practice

Try experimenting with the box model:

Continue Learning

Quick Check

Which part of the box model is the space between the content and the border?