JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

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

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionscss
Next

Learn the concept

The CSS Box Model

css
junior
box-model

Describe the CSS box model and how it affects element sizing.

box-model
box-sizing
margin
padding
border
layout
Quick Answer

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.

Detailed Explanation

The CSS box model defines how every HTML element is rendered as a rectangular box.

The four layers (inside out):

  1. Content — The actual content (text, images)
  2. Padding — Transparent space between content and border
  3. Border — Visible edge around the padding
  4. Margin — Transparent space outside the border

box-sizing property:

  • content-box (default): width = content only. Total width = width + padding + border
  • border-box: width = content + padding + border. Much more intuitive!

Margin collapsing:

  • Vertical margins between adjacent elements collapse to the larger value
  • Parent-child margins can collapse if no padding/border separates them
  • Horizontal margins never collapse

Best practices:

  • Use box-sizing: border-box globally — it's what every modern CSS reset and framework does.
  • Consider logical properties (margin-block, padding-inline) for styles that adapt to international writing modes.

Code Examples

Box model and box-sizingCSS
/* Global reset — every modern project does this */
*, *::before, *::after {
  box-sizing: border-box;
}

/* content-box (default) */
.content-box {
  box-sizing: content-box;
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  /* Total width: 200 + 20*2 + 5*2 = 250px */
}

/* border-box (recommended) */
.border-box {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  /* Total width: 200px (content shrinks to 150px) */
}

Real-World Applications

Use Cases

Building a responsive card component

Where each card needs consistent padding and border, and you want its overall width to be predictable regardless of these internal spaces.

Creating a navigation bar

Where menu items have internal padding but you want their total width to respect a parent container's layout without overflow.

Designing a modal dialog box

That needs precise control over its dimensions, including padding and borders, to fit perfectly within the viewport.

Mini Projects

Responsive Image Gallery

beginner

A gallery of images that adapts to different screen sizes and uses the box model for consistent spacing and sizing of image containers.

Custom Button Styles with Hover Effects

beginner

Design and implement custom buttons with interactive hover effects, utilizing the box model to manage their dimensions and internal spacing.

Industry Examples

GitHub

GitHub uses the box model extensively for layout and component sizing across its interface. For instance, user profile cards, code blocks, and navigation elements all leverage `box-sizing: border-box` to maintain consistent dimensions and spacing.

Stripe

Stripe's dashboard and payment forms rely on a well-defined box model to ensure that input fields, buttons, and informational cards are rendered consistently across different browsers and devices, providing a polished user experience.

Resources

MDN - The box model

docs

CSS-Tricks - The CSS Box Model

article

MDN - CSS Logical Properties

docs

Related Questions

How does CSS Flexbox work and when should you use it?

junior
flexbox
Next
How does CSS Flexbox work and when should you use it?
Next