Box Model and Display
Learn content, padding, border, margin, box-sizing, and the display values that control how elements behave in layout.
Learn content, padding, border, margin, box-sizing, and the display values that control how elements behave in layout. This hands-on tutorial focuses on practical implementation of box model and display concepts.
Box Model and Display
Every element on a page is a box. Once you understand that box, spacing bugs become much easier to fix.
The Four Layers
The CSS box model includes:
- content
- padding
- border
- margin
.card {
width: 320px;
padding: 24px;
border: 1px solid #cbd5e1;
margin: 16px auto;
}
Without box-sizing: border-box, the real rendered width becomes wider than 320px because padding and border are added outside the content box.
Use border-box
*,
*::before,
*::after {
box-sizing: border-box;
}
This makes sizing more predictable because declared width includes padding and border.
Display Values You Will Use Most
.block { display: block; }
.inline { display: inline; }
.inline-block { display: inline-block; }
.flex { display: flex; }
.grid { display: grid; }
.none { display: none; }
Practical Notes
blockelements take full available width by default.inlineelements do not accept width and height in the same way.inline-blockmixes flow behavior with box sizing.flexandgridcreate modern layout contexts.
Debugging Spacing Faster
When a layout looks wrong, inspect:
- the element width
- the computed margin and padding
- the parent display mode
- whether
box-sizingis in effect
Next Step
Continue to Flexbox and Grid to build layouts instead of just styling individual boxes.