The CSS Box Model

In CSS, the term “box model” is used when talking about web design and layout.

The CSS box model is essentially a box that wraps around every HTML element.

Every box consists of four parts: content, padding, borders and margins.

The image below illustrates the CSS box model:

Explanation of the different parts (from innermost part to outermost part):

The box model allows us to add a border around elements, and to define space between elements.

Example

Demonstration of the box model:

Demonstrating the Box Model

The CSS box model is essentially a box that wraps around every HTML element.

It consists of (from innermost part to outermost part): content, padding, border, and margin.

CSS Margins

The CSS margin properties are used to create space around elements, outside of any defined borders.

Margins define the distance between an element’s border and the surrounding elements.

With CSS, you have full control over the margins. CSS has properties for setting the margin for each individual side of an element (top, right, bottom, and left), and a shorthand property for setting all the margin properties in one declaration.

Margin – Individual Sides

CSS has properties for specifying the margin for each side of an element:

All the margin properties can have the following values:

  • auto – the browser calculates the margin
  • length – specifies a margin in px, pt, cm, etc.
  • % – specifies a margin in % of the width of the containing element
  • inherit – specifies that the margin should be inherited from the parent element

Tip: Negative values are also allowed.

Example

Set different margins for all four sides of a <p> element:

Margin – Shorthand Property

To shorten the code, it is possible to specify all the margin properties in one declaration.

The margin property is a shorthand property for the following individual margin properties:

Here is how it works:

If the margin property has four values:

  • margin: 25px 50px 75px 100px;
    • top margin is 25px
    • right margin is 50px
    • bottom margin is 75px
    • left margin is 100px

Example

Use the margin shorthand property with four values:

CSS Padding

The CSS padding properties are used to generate space around an element’s content, inside of any defined borders.

With CSS, you have full control over the padding. There are properties for setting the padding for each side of an element (top, right, bottom, and left), and a shorthand property for setting all the padding properties in one declaration.

Padding – Individual Sides

CSS has properties for specifying the padding for each side of an element:

All the padding properties can have the following values:

  • length – specifies a padding in px, pt, cm, etc.
  • % – specifies a padding in % of the width of the containing element
  • inherit – specifies that the padding should be inherited from the parent element

Note: Negative values are not allowed.

Example

Set different padding for all four sides of a <div> element:

Padding – Shorthand Property

To shorten the code, it is possible to specify all the padding properties in one declaration.

The padding property is a shorthand property for the following individual padding properties:

Here is how it works:

If the padding property has four values:

  • padding: 25px 50px 75px 100px;
    • top padding is 25px
    • right padding is 50px
    • bottom padding is 75px
    • left padding is 100px

Example

Use the padding shorthand property with four values:

Padding and box-sizing

The box-sizing property defines how the width and height of an element are calculated: should they include padding and borders, or not.

The box-sizing property can have the following values:

  • content-box – This is default. The width and height properties includes only the content (border and padding are not included)
  • border-box – The width and height properties includes content, padding and border

So, to keep the width at 300px, no matter the amount of padding, you can use the box-sizing: border-box;. This causes the element to maintain its actual width; if you increase the padding, the available content space will decrease.

Example

Use the box-sizing property to keep the width at 300px, no matter the amount of padding: