Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - CSS Box Model


CSS Box Model

Every HTML element on a web page is treated as a box. The CSS Box Model describes how these boxes are structured and how space is calculated.


The Four Parts of the Box Model

Each box consists of four layers:

  • Content - The text or image inside the element.
  • Padding - The space between the content and the border.
  • Border - The edge that surrounds the padding and content.
  • Margin - The space outside the border, separating it from other elements.

The image below illustrates the box model visually:

Example

Basic box model structure:

div {
  width: 300px;
  padding: 20px;
  border: 5px solid gray;
  margin: 25px;
}
Try it Yourself »

In this example:

  • Content width = 300px
  • Padding adds 40px total (20px left + 20px right)
  • Border adds 10px total (5px left + 5px right)
  • Margin adds 50px total (25px left + 25px right)

So the total element width is 300 + 40 + 10 = 350px.

Tip: Use browser developer tools (F12) to inspect elements and visualize their box model directly.


Width and Height

By default, the width and height of an element apply only to the content box - not including padding, borders, or margins.

Example

Default sizing:

div {
  width: 300px;
  padding: 20px;
  border: 10px solid gray;
}
Try it Yourself »

The total width = 300 (content) + 40 (padding) + 20 (border) = 360px.


Box-Sizing Property

You can change how the browser calculates an element's total size using box-sizing.

  • content-box - (default) width/height include only content.
  • border-box - width/height include content, padding, and border.

Example

Using border-box:

div {
  box-sizing: border-box;
  width: 300px;
  padding: 20px;
  border: 10px solid gray;
}
Try it Yourself »

Now the total width remains 300px because padding and borders are included within that size.


Global Box-Sizing Reset

A common practice is to apply box-sizing: border-box to all elements for predictable layouts.

Example

Reset box-sizing:

* {
  box-sizing: border-box;
}
Try it Yourself »

Tip: Most modern CSS frameworks (like W3.CSS or Bootstrap) use box-sizing: border-box globally for easier sizing.


Margin and Padding in Box Model

Padding and border are part of the box's size. Margins add extra space outside the box.

Example

Comparison of spacing:

div.one { padding: 20px; background: lightyellow; }
div.two { margin: 20px; background: lightblue; }
Try it Yourself »

Box Model Summary

PropertyDescription
contentThe text or image inside the element
paddingClears space inside the border
borderSurrounds padding and content
marginClears space outside the border

Best Practices

  • The box model controls layout and spacing in every web design.
  • Use box-sizing: border-box for simpler layout control (to include padding and border in total width).
  • Use margins for outer spacing and padding for inner spacing.
  • Inspect element boxes with browser dev tools to debug layouts.
  • Be aware that padding and border increase total box size in content-box mode.

Next, you'll learn about CSS Display - how elements behave in the layout (block, inline, inline-block, and none).

Next » CSS Display


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.