JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeTopicscssThe CSS Box Model
PrevNext
css
beginner
8 min read

The CSS Box Model

border
border-box
box-model
box-sizing
content-box
fundamentals
layout
margin
margin-collapsing
padding

Every element is a rectangular box composed of content, padding, border, and margin layers — and the box-sizing property determines whether width and height apply to just the content (content-box) or include padding and border (border-box).

Key Points

1Four Layers

Every element box has content (innermost), padding, border, and margin (outermost). Padding gets the background color; margin is always transparent.

2box-sizing Property

content-box (default) applies width/height to content only, so padding and border increase total size. border-box includes padding and border in the declared width/height.

3Universal border-box Reset

Nearly all modern projects apply box-sizing: border-box to all elements because it makes sizing predictable — the element never exceeds its declared dimensions.

4Margin Collapsing

Adjacent vertical margins merge into the larger value rather than adding together. This does not happen in flex/grid containers or when padding/border separates parent and child.

What You'll Learn

  • Describe the four layers of the CSS box model and how each affects element sizing
  • Explain the difference between content-box and border-box and why border-box is preferred
  • Predict when margin collapsing will occur and how to prevent it
  • Use browser DevTools to inspect and debug box model dimensions

Deep Dive

The CSS box model is one of the most fundamental concepts in web layout and a staple of front-end interviews. Every HTML element rendered on the page generates a rectangular box, and the box model defines how that box's dimensions are calculated.

The Four Layers

From inside out, every box has four areas:

  1. Content — The actual text, images, or child elements. Sized by width and height (or intrinsic sizing).
  2. Padding — Transparent space between the content and the border. Set with padding (shorthand) or individual sides. Padding is inside the border and receives the element's background color.
  3. Border — A visible (or invisible) edge around the padding. Set with border-width, border-style, and border-color. Borders add to the element's visual size by default.
  4. Margin — Transparent space outside the border that separates the element from its neighbors. Margins do not receive background color. Adjacent vertical margins collapse — the larger margin wins, rather than both being added.

box-sizing: content-box vs border-box

The box-sizing property changes how width and height are calculated:

  • content-box (default): width and height apply only to the content area. Total rendered width = width + padding-left + padding-right + border-left + border-right. This means adding padding or border makes the element larger than the declared width, which frequently causes layout surprises.

  • border-box: width and height include content + padding + border. The content area shrinks to accommodate padding and border within the declared width. This model is more intuitive because the element never exceeds its declared size.

The universal reset *, *::before, *::after { box-sizing: border-box; } is used in virtually every modern project (Normalize.css, Tailwind CSS, Bootstrap, and most resets include it). This is one of the most commonly asked "why" questions in interviews.

Margin Collapsing

Vertical margins between adjacent block-level elements collapse: if one element has margin-bottom: 20px and the next has margin-top: 30px, the gap between them is 30px (the larger value), not 50px. Margins also collapse between a parent and its first/last child if there is no padding, border, or content separating them. This behavior only applies to vertical (block-direction) margins — horizontal margins never collapse.

Margin collapsing does not occur in flex containers, grid containers, or elements with overflow other than visible.

The Outline Layer

Outlines (outline property) sit outside the border but do not affect layout or box dimensions. They are commonly used for focus indicators and accessibility. Unlike borders, outlines can be offset with outline-offset.

Inspecting the Box Model

Every browser's DevTools shows a visual box model diagram. In Chrome, select an element and look at the Computed tab to see content, padding, border, and margin values in a nested rectangle visualization. This is an essential debugging tool.

Common Interview Questions

Interviewers frequently ask: "What is the CSS box model?", "What is the difference between content-box and border-box?", "Why do most resets use border-box?", and "Explain margin collapsing." Understanding these thoroughly demonstrates solid CSS fundamentals.

Fun Fact

Internet Explorer 5 actually used border-box by default, which the standards community initially considered a bug. Years later, the industry adopted border-box universally — IE5 was right all along.

Continue Learning

CSS Positioning & Stacking Context

beginner

Flexbox Layout

beginner

CSS Grid Layout

intermediate

Practice What You Learned

Describe the CSS box model and how it affects element sizing.
junior
box-model
Every element is a rectangular box with four layers: content (the actual text/image), padding (space between content and border), border (the visible edge), and margin (space outside the border). By default, width/height set only the content size; use box-sizing: border-box to include padding and border.
Previous
Transitions & Keyframe Animations
Next
Cascade Layers (@layer)
PrevNext