HTML & CSS Masterclass Tutorial

Comprehensive guide to modern HTML and CSS development.

Box Model

The CSS Box Model

The Box Model is arguably the most important concept in CSS. Every element on a webpage is a rectangular box. The box consists of four layers (from inside to outside):

  1. Content: The actual content (text, image).
  2. Padding: Clears an area around the content. Padding is transparent and sits inside the border.
  3. Border: A border that goes around the padding and content.
  4. Margin: Clears an area outside the border. Margin is transparent and pushes other elements away.

box-sizing: border-box

By default, adding padding and borders makes the element wider than the width you set. By setting box-sizing: border-box;, padding and borders are included in the element's total width and height. This is a standard practice on modern websites.

Try It Yourself

Edit the code below and click Run to see the result live.

Source Code
Result
Topics