Code has been added to clipboard!

CSS Box Model: Learn About the Border Box in CSS and Other Components

Reading time 3 min
Published Sep 9, 2016
Updated Jan 21, 2020

TL;DR – Browsers see an HTML element as a rectangular box known as the CSS box model. It includes four components: borders, padding, margins, and content.

What the box model is

Every HTML element is a box. The CSS box model helps beginners to understand the layout and web page design better.

The box model consists of several components, such as padding and margins. CSS sets the position, size, and other properties to these boxes.

Properties of the box model CSS

In the example below, you see all the properties that make up the CSS box model:

Example
div {
    width: 200px;
    border: 35px solid blue;
    padding: 35px;
    margin: 35px;
}

Border box CSS

The border box CSS is between the margin and padding components of the box model. The border shorthand lets you set the width, style, and color properties in on declaration.

The following code example produces borders that are 3px in thickness, dotted, and blue:

Example
p {
   border: 3px dotted blue;
}

Note: if developers do not define the border box CSS, it is set to 0, and the border is invisible.

Padding box model

Padding box model in CSS refers to the space between the border and the element content. The padding shorthand defines all four sides of an element in one declaration.

This example shows how to define all four sides of an element with one value:

Example
p {
   padding: 30px;
}

Note: you can also use longhand properties such as padding-top, padding-right, padding-bottom, and padding-left to define separate sides individually.

Margin box

The final component in the CSS and HTML box model is the margin. The margin is outside an element, and it surrounds other boxes with space.

The margin shorthand defines the space for all four sides of an element in one declaration. Look at this example:

Example
p {
    margin: 10px 20px 30px 70px; 
}

Note: the margin collapsing happens when margins of separate elements touch each other. It ends with the creation of one margin.

Content box

The content box defines the area for text, links, images, or other element content. The width and height properties define the width and height of the content box.

Remember: you can define the width and height of content boxes with other properties. max-width, min-width, max-height, and min-height set constraints and not a fixed size.

You can calculate the total element width like this:
right margin + left margin + right border + left border + right padding + left padding + width.

To calculate the total element height, follow this rule:
bottom margin + top margin + bottom border + top border + bottom padding + top padding + height.

In this example, we style a <div> element to have a 400 px total width. Here is the math to count the total width of a box: 330px (width) + 30px (right + left padding) + 40px (right + left border) + 0px (right + left margin) = 400px.

Example
div {
    width: 330px;
    padding: 30px;
    border: 40px solid blue;
    margin: 0; 
}

CSS box model: useful tips

  • You can analyze the CSS box model in every website by using Chrome DevTools.
  • By setting the box-sizing property to border-box value, you indicate that the padding and the border are a part of the total width.
Tutorial
CSS3 Features
Syntax
Classes
ID Selectors
Attribute Selectors
Stylesheets
Inline
Internal
External
Box Model
Children Selectors
Pseudo Classes
Pseudo Elements
Variables
Counters
Text
Fonts
Web Fonts
Backgrounds
Background Images
Colors
Gradients
Opacity / Transparency
Shadow Effects
Transitions
Tooltip
Transform
Animations
Layout — Display
Layout — Position
Layout — Float
Layout — Clear
Layout — Horizontal & Vertical Align
Multiple Columns
Introduction
Responsive Web Design — Viewport
Responsive Web Design — Grid View
Responsive Web Design — Media Queries
Responsive Web Design — Flexbox Layout
Responsive Web Design — Images
Responsive Web Design — Videos
Borders
Margin
Padding
Width
Height
Outline
Links
Lists
Tables
Dropdown Menu
Navigation Bar
Images
Image Gallery
Border Images
Forms
Rounded Corners
Buttons
Box-Sizing
Selector Reference
Pagination Examples
Code Examples
CSS3 Browser Support Reference
Functions Reference
Speech Module Reference
Units
Web Safe Font Combinations
Cheat Sheet
:hover
@font-face
@keyframes
@media
align-content
align-items
align-self
all
animation
backface-visibility
background
background-clip
background-color
background-image
background-origin
background-position
background-size
border
border-image
border-radius
border-style
box-shadow
box-sizing
color
columns
filter
flex
flex-basis
flex-direction
flex-flow
flex-grow
flex-shrink
flex-wrap
font
font-family
font-size
font-size-adjust
font-stretch
font-style
font-weight
hanging-punctuation
justify-content
line-height
margin
offset
opacity
order
outline
overflow
padding
perspective
position
resize
tab-size
text-align
text-decoration
text-emphasis
text-transform
text-shadow
transform
transition-property
translate
vertical-align
word-break
word-wrap
z-index