The Box Model
Control content, padding, borders, sizing, overflow, and spacing with confidence.
Every rendered element occupies a box: content surrounded by padding, a border, and margin. Layout becomes predictable when declared dimensions include the parts you expect.
Use border-box sizing
*, *::before, *::after {
box-sizing: border-box;
}
With border-box, a declared width includes content, padding, and border. Without it, padding and border are added outside the width.
.card {
width: min(100%, 32rem);
padding: 1.5rem;
border: 1px solid #ded9e1;
margin-inline: auto;
}
Block, inline, and flow
Block boxes normally fill available inline space and start on a new line. Inline boxes flow with text. inline-block, flex, and grid create different formatting contexts.
Logical properties and overflow
Logical properties follow writing direction, making layouts more adaptable.
.notice {
padding-block: 1rem;
padding-inline: 1.25rem;
border-inline-start: 3px solid #f46537;
overflow-wrap: anywhere;
}
Use overflow: auto for intentionally scrollable regions. Avoid hiding overflow simply to conceal a layout bug.
Keep this
Set global border-box, use logical properties, and distinguish internal padding from external layout spacing.