CSS Fundamentals

Every element is
a box.

The CSS Box Model is the foundation of web layout. Learn it once, understand everything. This is your visual guide.

Think of it like a gift box.

Imagine wrapping a gift. You place the gift inside, add some bubble wrap so it doesn't rattle, put it in a box with thick walls, and then leave some space between that box and other packages.

That's exactly what CSS does with every element on a webpage. The gift is your content. The bubble wrap is padding. The box walls are border. The space between packages is margin.

What makes up a box?

Every HTML element is surrounded by four invisible layers. Together they control size, spacing, and appearance.

📄
Content

The actual stuff inside — text, images, video. You control its size with width and height.

Content Layer
🛡️
Padding

Breathing room inside the border. It pushes the border away from the content. Inherits the background color.

Padding Layer
🖼️
Border

A line that wraps around the padding. Can be solid, dashed, dotted, or even invisible. Has its own width and color.

Border Layer
↔️
Margin

Space outside the border — the gap between this element and everything else. It's always transparent.

Margin Layer

Box Model Playground

Drag the sliders to reshape the box. Watch each layer respond in real time.

🎛️ Interactive Box Model
box-sizing:
content-box border-box
Content Width 160px
Content Height 100px

Padding 20px
Border 8px
Margin 24px

Total Element Size
Width
Height
margin
border
padding
160×100 content
Content
160×100
+ Padding
40px
+ Border
16px
+ Margin
48px

Interactive DIV Playground

See the box model on a real <div> element. The diagram is not abstract — this is exactly how your CSS behaves.

💻 Interactive DIV Playground <div>
Sliders update CSS in real time
CSS Properties
width 200px
height 120px

padding 20px
border-width 4px
margin 24px
💡 Did you know? Padding and border add to the element's total size in content-box mode. Use border-box to keep dimensions predictable.
margin
<div>
your content here
margin
border
padding
content
style.css
div { width: 200px; height: 120px; padding: 20px; border: 4px solid #558B71; margin: 24px; } /* Move sliders to see CSS update live! */

Ready to go deeper?

Understand the why behind the box model.

Margin collapsing, border-box vs content-box, browser rendering — the deep dive awaits.

Take the Deep Dive →