<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/the-css-box-model-padding-border-margin-and-box-sizing-ibjuxvwyw" -->

---
title: The CSS Box Model: Padding, Border, Margin, and box-sizing
description: By default, CSS width and height size only the content, so padding and borders make a box bigger than the number you wrote. A 200-pixel card with 20 pixels of...
canonical: https://daily.dev/posts/the-css-box-model-padding-border-margin-and-box-sizing-ibjuxvwyw
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: The CSS Box Model: Padding, Border, Margin, and box-sizing | daily.dev
og:description: By default, CSS width and height size only the content, so padding and borders make a box bigger than the number you wrote. A 200-pixel card with 20 pixels of...
og:url: https://daily.dev/posts/the-css-box-model-padding-border-margin-and-box-sizing-ibjuxvwyw
og:image: https://api.daily.dev/og/posts/iBjUxVwyw.png
og:image:alt: The CSS Box Model: Padding, Border, Margin, and box-sizing
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# The CSS Box Model: Padding, Border, Margin, and box-sizing

**[Kirupa](https://daily.dev/sources/kirupa)** · 13 min read · 0 upvotes · 0 comments

## Summary

By default, CSS width and height size only the content, so padding and borders make a box bigger than the number you wrote. A 200-pixel card with 20 pixels of padding and a 5-pixel border ends up 250 pixels wide on screen. Setting box-sizing: border-box makes the width cover padding and border, so the same card stays 200 pixels and its content shrinks to 150. Vertical margins between stacked blocks collapse to the larger one: 30 and 20 pixels give a 30-pixel gap, except in flex and grid containers. Inline elements ignore width and height, except images and form controls, which respect width, height and vertical margins.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.kirupa.com/html5/css_box_model.htm>

## Questions this post answers

### Why is my CSS div wider than the width I set when I add padding and a border?

By default, box-sizing is content-box, so width and height size only the content. Padding and border are added on top. A 200px-wide element with 20px of padding and a 5px border on each side renders 250px wide (200 + 40 + 10). Setting box-sizing: border-box makes the width include padding and border, so it stays 200px.

_Developers untangling CSS layout surprises like this follow web fundamentals on daily.dev._

### Why is the gap between my heading and paragraph smaller than the sum of their margins?

Vertical margins between stacked block elements collapse, and only the larger one counts. A heading with margin-bottom: 30px above a paragraph with margin-top: 20px produces a 30px gap, not 50px. Margins do not collapse inside flex or grid containers, where children keep both margins in full, and left and right margins always add up.

_Anyone debugging spacing in CSS layouts keeps sharp on daily.dev._

### How do I stop a child's top margin from leaking out of its parent in CSS?

A child's top margin collapses through its parent when nothing separates the parent's top edge from that margin, so it appears above the parent. Giving the parent padding, a border, or display: flow-root keeps the margin inside it. Putting the parent in a flex or grid container also prevents margins from collapsing.

_Developers chasing down stubborn CSS spacing bugs stay current on daily.dev._

## Similar posts on daily.dev

- [Understanding CSS Text-Box: Simplifying Padding Solutions](https://daily.dev/posts/understanding-css-text-box-simplifying-padding-solutions-jegps7bip) · Frontend Masters · 7 upvotes · 0 comments
- [CSS: Unavoidable Bad Parts](https://daily.dev/posts/css-unavoidable-bad-parts-ia9uwkcoo) · matklad · 15 upvotes · 0 comments
- [The CSS aspect-ratio property and the math behind it](https://daily.dev/posts/the-css-aspect-ratio-property-and-the-math-behind-it-zuquamsi6) · Flavio Copes · 3 upvotes · 1 comments
- [Trimming the air above a capital letter](https://daily.dev/posts/trimming-the-air-above-a-capital-letter-vtr5n9g8g) · Creative Design Technologist · 3 upvotes · 3 comments

---

Tags: [#webdev](https://daily.dev/tags/webdev), [#css](https://daily.dev/tags/css)

[View this post on daily.dev](https://daily.dev/posts/the-css-box-model-padding-border-margin-and-box-sizing-ibjuxvwyw)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"The CSS Box Model: Padding, Border, Margin, and box-sizing","url":"https://daily.dev/posts/the-css-box-model-padding-border-margin-and-box-sizing-ibjuxvwyw","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/the-css-box-model-padding-border-margin-and-box-sizing-ibjuxvwyw"},"datePublished":"2026-11-30T23:36:31.886Z","dateModified":"2026-11-30T23:36:50.915Z","description":"By default, CSS width and height size only the content, so padding and borders make a box bigger than the number you wrote. A 200-pixel card with 20 pixels of...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/e3fb6fbc979774a44b05fee314d1ec70?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/e3fb6fbc979774a44b05fee314d1ec70?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Kirupa","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kirupa","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ff2fdb4927d34b81bd07cd18d6049a98","url":"https://daily.dev/sources/kirupa"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/the-css-box-model-padding-border-margin-and-box-sizing-ibjuxvwyw","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"webdev,css","timeRequired":"PT13M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kirupa","item":"https://daily.dev/sources/kirupa"},{"@type":"ListItem","position":3,"name":"The CSS Box Model: Padding, Border, Margin, and box-sizing"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/the-css-box-model-padding-border-margin-and-box-sizing-ibjuxvwyw#faq","mainEntity":[{"@type":"Question","name":"Why is my CSS div wider than the width I set when I add padding and a border?","acceptedAnswer":{"@type":"Answer","text":"By default, box-sizing is content-box, so width and height size only the content. Padding and border are added on top. A 200px-wide element with 20px of padding and a 5px border on each side renders 250px wide (200 + 40 + 10). Setting box-sizing: border-box makes the width include padding and border, so it stays 200px. Developers untangling CSS layout surprises like this follow web fundamentals on daily.dev."}},{"@type":"Question","name":"Why is the gap between my heading and paragraph smaller than the sum of their margins?","acceptedAnswer":{"@type":"Answer","text":"Vertical margins between stacked block elements collapse, and only the larger one counts. A heading with margin-bottom: 30px above a paragraph with margin-top: 20px produces a 30px gap, not 50px. Margins do not collapse inside flex or grid containers, where children keep both margins in full, and left and right margins always add up. Anyone debugging spacing in CSS layouts keeps sharp on daily.dev."}},{"@type":"Question","name":"How do I stop a child's top margin from leaking out of its parent in CSS?","acceptedAnswer":{"@type":"Answer","text":"A child's top margin collapses through its parent when nothing separates the parent's top edge from that margin, so it appears above the parent. Giving the parent padding, a border, or display: flow-root keeps the margin inside it. Putting the parent in a flex or grid container also prevents margins from collapsing. Developers chasing down stubborn CSS spacing bugs stay current on daily.dev."}}]}
```

