CSS Box Model

Understand how elements are sized and spaced in CSS - the foundation of layout.

What is the Box Model?

Every HTML element is treated as a box in CSS. The CSS box model describes how the content, padding, border, and margin work together to create the visual representation of an element.

Box Model Components:

  • Content: The actual content of the element (text, images, etc.)
  • Padding: Clear space around the content, inside the border
  • Border: A border around the padding and content
  • Margin: Clear space outside the border

Box Model Visualization

Interactive Box Model Demo

Content Area

Yellow: Margin | Green: Border | Blue: Padding | Orange: Content

Box Model Properties

Content - Width and Height

Control the size of the content area.

/* Fixed dimensions */
.element {
    width: 200px;
    height: 100px;
}

/* Percentage dimensions */
.container {
    width: 100%;
    height: 50vh; /* 50% of viewport height */
}

/* Auto dimensions */
.auto-width {
    width: auto; /* Default - fits content */
    height: auto;
}

/* Min and Max dimensions */
.responsive {
    min-width: 200px;
    max-width: 800px;
    min-height: 100px;
    max-height: 400px;
}

Padding - Internal Spacing

Add space inside the element, between content and border.

/* All sides */
.element {
    padding: 20px;
}

/* Individual sides */
.element {
    padding-top: 10px;
    padding-right: 20px;
    padding-bottom: 15px;
    padding-left: 25px;
}

/* Shorthand - top/bottom, left/right */
.element {
    padding: 10px 20px;
}

/* Shorthand - top, left/right, bottom */
.element {
    padding: 10px 20px 15px;
}

/* Shorthand - top, right, bottom, left (clockwise) */
.element {
    padding: 10px 20px 15px 25px;
}

Padding Demo

Small padding (5px)

Large padding (20px)

Different horizontal/vertical padding

Border - Element Boundaries

Create borders around elements.

/* Border shorthand */
.element {
    border: 2px solid #333;
}

/* Individual border properties */
.element {
    border-width: 3px;
    border-style: solid;
    border-color: #007bff;
}

/* Individual sides */
.element {
    border-top: 1px solid red;
    border-right: 2px dashed blue;
    border-bottom: 3px dotted green;
    border-left: 4px double orange;
}

/* Border radius for rounded corners */
.rounded {
    border-radius: 10px;
    border: 2px solid #333;
}

/* Different radius for each corner */
.custom-radius {
    border-radius: 10px 20px 30px 40px;
}

Border Demo

Solid border

Dashed border

Dotted border

Double border

Rounded border

Margin - External Spacing

Add space outside the element, between elements.

/* All sides */
.element {
    margin: 20px;
}

/* Individual sides */
.element {
    margin-top: 10px;
    margin-right: 20px;
    margin-bottom: 15px;
    margin-left: 25px;
}

/* Shorthand - top/bottom, left/right */
.element {
    margin: 10px 20px;
}

/* Auto margins for centering */
.centered {
    margin: 0 auto; /* Centers block elements */
    width: 200px;
}

/* Negative margins */
.overlap {
    margin-top: -10px; /* Can overlap other elements */
}

Margin Demo

Small margin (5px)

Large margin (20px)

Different horizontal/vertical margin

Box Sizing

Understanding Box Sizing

The box-sizing property determines how the width and height are calculated.

Two Box Sizing Models:

  • content-box (default): Width/height only includes content
  • border-box: Width/height includes content, padding, and border

Box Sizing Comparison

Content Box (Default)

Width: 200px + 40px padding + 10px border = 250px total

Total width: 250px

Border Box

Width: 200px (includes padding & border)

Total width: 200px

Box Sizing Code Examples

/* Default behavior */
.content-box {
    box-sizing: content-box;
    width: 200px;
    padding: 20px;
    border: 5px solid #333;
    /* Total width = 200 + 40 + 10 = 250px */
}

/* Modern approach */
.border-box {
    box-sizing: border-box;
    width: 200px;
    padding: 20px;
    border: 5px solid #333;
    /* Total width = 200px */
}

/* Global reset for easier calculations */
* {
    box-sizing: border-box;
}

Display Properties

Common Display Values

/* Block elements */
.block {
    display: block;
    /* Takes full width, creates line breaks */
}

/* Inline elements */
.inline {
    display: inline;
    /* Takes only needed width, no line breaks */
}

/* Inline-block elements */
.inline-block {
    display: inline-block;
    /* Behaves like inline but respects width/height */
}

/* Flexbox */
.flex {
    display: flex;
    /* Creates a flex container */
}

/* CSS Grid */
.grid {
    display: grid;
    /* Creates a grid container */
}

/* Hidden elements */
.hidden {
    display: none;
    /* Removes element from layout */
}

Display Demo

Block
Inline Inline
Inline-Block
Inline-Block
Flex
Grid

Notice how each display type affects the layout and spacing

Common Box Model Issues

1. Margin Collapse

Vertical margins between adjacent elements collapse to the larger value.

/* These margins will collapse */
.box1 {
    margin-bottom: 20px;
}

.box2 {
    margin-top: 30px;
}
/* Result: 30px space between boxes, not 50px */

2. Width Calculations

Without border-box, padding and borders add to the specified width.

/* This might not work as expected */
.container {
    width: 100%;
    padding: 20px;
    border: 1px solid #333;
}
/* Total width = 100% + 40px + 2px = over 100% */

3. Inline Elements and Box Model

Inline elements don't respect width, height, or vertical margins.

/* This won't work on inline elements */
span {
    width: 100px; /* Ignored */
    height: 50px; /* Ignored */
    margin-top: 20px; /* Ignored */
    margin-bottom: 20px; /* Ignored */
}

Best Practices

Box Model Best Practices

  • Use border-box: Set box-sizing: border-box globally for easier calculations
  • Understand margin collapse: Be aware that vertical margins collapse between adjacent elements
  • Use appropriate units: Use relative units (%, em, rem) for responsive design
  • Consider the display property: Different display values affect how the box model works
  • Test on different browsers: Box model behavior can vary slightly between browsers
  • Use CSS reset: Normalize default box model behavior across browsers

Recommended CSS Reset

/* Modern CSS Reset */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Ensure consistent box model */
html {
    box-sizing: border-box;
}

*,
*::before,
*::after {
    box-sizing: inherit;
}