DevLab
CSS

The CSS Box Model: margin, padding, border, and content

Understand how every HTML element is rendered as a rectangular box, how margin and padding differ, and how box-sizing:border-box changes layout forever.

Every Element is a Box

In CSS, every HTML element generates a rectangular box with four regions, from inside to outside: content (where text and child elements live), padding (space between content and border, has background color), border (visible or invisible line around padding), and margin (transparent space outside the border separating elements).

The Default box-sizing Trap

By default, CSS uses box-sizing: content-box. A 200px wide div with padding: 20px; border: 2px solid; actually occupies 244px total (200 + 20×2 + 2×2). This is the source of countless layout bugs.

/* The fix — apply globally at the start of every project */
*, *::before, *::after {
  box-sizing: border-box;
}

/* Now this div is exactly 200px wide total */
.card {
  width: 200px;
  padding: 20px;
  border: 2px solid #ccc;
}

Margin vs Padding

Padding is space inside the element (part of the clickable area, has background color). Margin is space outside (affects layout, transparent, collapses with adjacent margins).

  • Space between text and button edge → padding
  • Space between two buttons → margin
  • Larger clickable area → padding (margin does not extend the click target)

Margin Collapse

When two vertical margins meet, they collapse into a single margin equal to the larger of the two — not the sum. This only happens with vertical margins, not horizontal ones, and does not occur inside flex or grid containers.

/* Two paragraphs have 30px gap, not 50px (20 + 30) */
p { margin-top: 20px; margin-bottom: 30px; }

Inspecting the Box Model

Open browser DevTools, select an element, and look at the Computed tab. The visual box model diagram shows exact margin, border, padding, and content dimensions — the fastest way to debug layout issues.

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedRegexRegex Quantifiers: Complete GuideCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines