Syntax
width, padding, border, marginExamples
See the layers
The preview card has a declared outer border-box width of 260px.
.card {
box-sizing: border-box;
width: 260px;
max-width: 100%;
padding: 24px;
border: 4px solid royalblue;
margin: 16px 0;
}The Four Layers
Each layer adds space around the one before it.
.box {
width: 200px; /* content width */
padding: 20px; /* space inside the border */
border: 2px solid #333; /* the border itself */
margin: 10px; /* space outside the border */
}
/* Total rendered width (standard box-sizing) = 200 + 40 + 4 + 20 = 264px */Visualizing with DevTools Colors
A common way to debug layout by temporarily outlining the box model layers.
.debug {
outline: 2px solid red; /* shows content edge without affecting layout */
background: rgba(0, 150, 255, 0.1); /* shows padding area */
}Parameters and values
- width / height (length | %): Sets the content box dimensions
- padding (length): Space between content and border
- border (width style color): The visible edge around the padding
- margin (length | auto): Space outside the border, between this element and others
Best practices
- Set box-sizing: border-box globally so padding and border are included in the declared width, avoiding surprise overflow
- Use margin for spacing between elements, and padding for spacing within an element
- Remember vertical margins between block elements can "collapse" into a single margin - the larger of the two, not their sum
- Use browser DevTools box model inspector when layout looks wrong - it visually breaks down all four layers
At a glance
- Purpose
- Presentation and layout
- File extension
- .css
- Runs in
- Web browsers
- Usually used with
- HTML and JavaScript
In plain English
Every element has a content box, with padding and a border around it. Margin creates space outside that border.
What you’ll learn
- Distinguish the four layers.
- Predict the rendered width.
- Choose a box-sizing model.
Before you start: HTML
Breaking down the syntax
content- The area for text, images and child content.
padding- Space between content and border.
border- The edge surrounding content and padding.
margin- Outer space beyond the border.
How it works
Content
The innermost area.
Padding + border
Added to a content-box width; included in a border-box width.
Margin
Outside the border and outside the declared width in both models.
When should I use this?
Use the box model whenever an element is too wide, spacing is unexpected, or a layout overflows.
Common mistakes
A full-width box with extra padding
With content-box, padding and border are added to width: 100%.
Incorrect
.card { width: 100%; padding: 24px; }Corrected
.card { box-sizing: border-box; width: 100%; padding: 24px; }Compare approaches
- content-box: width describes the content area.
- border-box: width includes padding and border.
Accessibility
- Keep text containers flexible so larger text can reflow.
- Use spacing to reinforce grouping without relying solely on colour.
Explore deeper
Margin is not always simple addition
Adjacent vertical margins in normal block flow can collapse. Flex and grid item margins do not collapse with each other. Inspect the layout context before adding more margin.
Specifications & further reading
Related CSS documentation
box-sizing controls how an element's total width and height are calculated. The default, content-box, means padding and border are added on top of the declared width, making sizing math confusing. border-box includes padding and border within the declared width instead, which is why nearly every modern CSS reset sets it globally.display Property
The display property determines how an element generates boxes and participates in layout. block elements start on a new line and fill available width. inline elements flow within text and ignore width/height. inline-block combines inline flow with block-like sizing control. none removes the element from layout entirely.position Property
The position property controls how an element is positioned in the document. static is the default, following normal flow. relative positions an element relative to its normal position. absolute removes it from flow entirely, positioning it relative to its nearest positioned ancestor. fixed positions relative to the viewport, staying in place during scroll. sticky toggles between relative and fixed based on scroll position.overflow Property
overflow controls what happens when content is too large to fit its container. visible (default) lets content spill out. hidden clips anything beyond the box. scroll always shows scrollbars. auto adds scrollbars only when needed. overflow-x and overflow-y control each axis independently.