The CSS Box Model: Content, Padding, Border and Margin
CSS Box Model: The Complete Guide to Content, Padding, Border and Margin
The CSS box model is the foundation of every web layout. Every HTML element is drawn as a rectangular box, and the box model defines how the size of that box is calculated and how much space it occupies on the page. Once you understand the CSS box model, you will know why elements sometimes become wider than expected, why spacing behaves in surprising ways, and how Flexbox, CSS Grid and responsive design really work under the hood.
What You Will Learn
- The four layers of the box model: content, padding, border and margin
- How total element width and height are calculated
- The difference between
box-sizing: content-boxandbox-sizing: border-box - How to write a modern CSS reset with border-box
- Border vs outline vs box-shadow, and block vs inline boxes
- CSS logical properties for international layouts
- How to debug the box model in browser DevTools
The Four Layers of the CSS Box Model
Every box is made of four layers, from the inside to the outside:
- Content: the area where your text, images or child elements live. Its size is controlled by
widthandheight. - Padding: the space between the content and the border. The element background is painted behind the padding.
- Border: a line or frame that wraps the padding and content, with its own thickness, style and color.
- Margin: the invisible space outside the border that pushes other elements away. Margin is always transparent.
Visual Diagram of the Box Model
+--------------------- MARGIN ---------------------+ | +------------------ BORDER ------------------+ | | | +-------------- PADDING --------------+ | | | | | | | | | | | CONTENT | | | | | | | | | | | +-------------------------------------+ | | | +---------------------------------------------+ | +---------------------------------------------------+
How Total Width and Height Are Calculated
By default, the width property only sets the size of the content box. Padding and border are added on top of it.
Total width = width + padding-left + padding-right + border-left + border-right Total height = height + padding-top + padding-bottom + border-top + border-bottom Space used = total size + margin on each side
Example: an element with width: 300px, padding: 20px and border: 5px solid is 350px wide by default (300 + 20 + 20 + 5 + 5). This is the number one reason layouts overflow their parent.
box-sizing: content-box vs border-box
The box-sizing property changes how width and height are calculated.
| Value | width applies to | Effect of adding padding or border |
|---|---|---|
| content-box (default) | Content only | The box grows bigger |
| border-box | Content + padding + border | The box keeps its size; the content area shrinks |
With box-sizing: border-box, a width: 300px element stays exactly 300px wide no matter how much padding or border you add. This makes percentage layouts, grids and responsive design far easier to predict. Margin is never included in either mode.
The Modern CSS Reset: Universal border-box
Almost every modern CSS reset and framework, including Tailwind CSS Preflight and Bootstrap, sets border-box globally. The classic snippet is *, *::before, *::after { box-sizing: border-box; }. A second pattern sets border-box on the root and lets everything inherit it, so a third-party component can opt out locally: html { box-sizing: border-box; } *, *::before, *::after { box-sizing: inherit; }. Note that box-sizing is not inherited by default, which is why the second pattern uses the inherit keyword.
Border vs Outline vs Box-shadow
- Border is part of the box model and changes the layout size.
- Outline is drawn outside the border edge and takes no space, so it never causes layout shift. It is ideal for focus rings. Use
outline-offsetto add a gap. - Box-shadow also takes no space and does not affect layout. A spread-only shadow such as
box-shadow: 0 0 0 3px bluecan imitate a border without changing size.
Never remove focus outlines without providing an accessible replacement (use :focus-visible).
Block, Inline and Inline-Block Boxes
- Block boxes (div, p, section) start on a new line, stretch to the available width and respect width, height, padding and margin on all sides.
- Inline boxes (span, a, strong) flow with text. They ignore width and height, and vertical margin has no effect. Vertical padding and border are painted but do not push neighbouring lines.
- Inline-block boxes flow like text but behave like blocks inside, so width, height and all spacing work.
Background and the Box Model
By default, the background is painted under the content, padding and border (background-clip: border-box). You can change this with background-clip: padding-box or content-box, and control where the background image starts with background-origin.
Logical Properties and the Box Model
Modern CSS adds logical properties that adapt to writing direction (left-to-right, right-to-left, vertical). Use margin-inline, margin-block, padding-inline, padding-block, inline-size and block-size instead of left, right, top and bottom. This gives proper support for right-to-left languages such as Urdu and Arabic.
Debugging the Box Model in DevTools
- Right-click any element and choose Inspect.
- Open the Computed tab (Chrome, Edge) or the Layout panel (Firefox) to see the box model diagram: content in blue, padding in green, border in yellow and margin in orange in Chrome.
- Hover over the element in the Elements panel to highlight its areas on the page.
- Edit values live in the Styles pane to test spacing before changing your code.
Common Box Model Mistakes
- Setting
width: 100%with padding in content-box mode, which causes horizontal overflow. - Expecting vertical margin to work on inline elements.
- Forgetting that vertical margins can collapse (covered in the margin lesson).
- Using borders for focus styles, which shifts layout. Use outline instead.
- Mixing border-box and content-box components without realising it.
Best Practices
- Always apply
box-sizing: border-boxglobally at the top of your stylesheet. - Use padding for space inside an element and margin or gap for space between elements.
- Prefer logical properties for new projects.
- Verify real rendered sizes with DevTools instead of guessing.
Frequently Asked Questions
What is the CSS box model in simple words?
Every element is a box made of content, padding, border and margin, and these layers decide how big the element is and how much space it takes.
Why is my div wider than the width I set?
Padding and border are added to the width in the default content-box mode. Switch to border-box.
Does margin count inside the element width?
No. Margin is outside the element, even in border-box mode.
Is border-box safe to use?
Yes. It is supported in every major browser and is the standard in modern projects.
Key Takeaways
- Box model layers: content, padding, border, margin.
- Default box-sizing is content-box, so padding and border add to the width.
- Use
*, *::before, *::after { box-sizing: border-box; }in every project. - Outline and box-shadow do not affect layout; border does.
- Inspect boxes in DevTools to debug spacing quickly.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
The card below is set to width 100% but it overflows its 400px parent because of padding and border. Fix it with one CSS property so the card fits exactly inside the parent, without changing its width, padding or border values.
Press Run to execute.
Show expected output
The card is exactly 400px wide in total and no horizontal overflow appears (box-sizing: border-box).This is a self-check — compare your result with the expected output above.
Write a function totalWidth(width, padding, border, boxSizing) that returns the total rendered width (without margin). Padding and border values are per side. For content-box add padding and border on both sides, for border-box return the width itself.
Press Run to execute.
Show expected output
350
300This is a self-check — compare your result with the expected output above.
Was this page helpful?