Search pages

Search all CSS Showcase pages by name

The Box Model

Everything in CSS is a box — master how they work and you'll master layouts

Box Model Fundamentals

The CSS box model describes how every element is rendered as a rectangular box, consisting of content, padding, border, and margin layers.

The Anatomy of a Box
Margin
Border
Padding
Content

The actual content of your element lives here

css
Padding
Uniform padding: 20px
Different padding per side
Shorthand: top/bottom left/right
css
Margin
Basic margin
Auto margin for centering
Negative margin overlap
css
Borders
Solid border
Dashed border
Gradient border trick
Rounded corners
css

Box Sizing Models

The box-sizing property changes how the total width and height of elements are calculated. This can make layouts much more predictable!

Content Box vs Border Box

content-box (default)

Width: 300px + padding + border

Total: 350px

border-box

Width: 300px total

Total: 300px
css
Practical Example
1/3
1/3
1/3
css

Interactive Box Model Playground

Experiment with different values to see how they affect the box model in real-time.

Adjust Properties

200px
100px
20px
5px
20px
Total: 250px × 150px
Content Area
.box {
    width: 200px;
    height: 100px;
    padding: 20px;
    border: 5px solid;
    margin: 20px;
    box-sizing: content-box;
}

Advanced Box Model Techniques

Take your box model skills to the next level with these advanced techniques and edge cases.

Margin Collapse
30px bottom margin
20px top margin

Gap = 30px (not 50px)

css
Outline vs Border
css
Percentage Calculations
50% width
10% padding
25% margin
css
Box Shadow Layers
Basic shadow
Inset shadow
Multiple shadows
Glow effect
css

Box Model Best Practices

Always Use Border-Box

Set box-sizing: border-box globally to make width calculations predictable and intuitive.

Margin for Spacing Between

Use margin for space between elements, padding for space inside elements.

Consistent Spacing Scale

Define spacing variables (8px, 16px, 24px…) for consistent rhythm throughout your design.

Continue Your Journey

Now that you understand how boxes work, explore how to arrange them!