The Box Model and Sizing
Reason about content, padding, borders, margins, and predictable element sizing.
Every rendered element participates in the box model. With border-box, the declared width includes padding and border, which makes component sizing predictable. Margins create outside separation while padding protects the component's inside space.
See the core pattern
Read this rule set from the outside in. Identify the inputs, the decision or transformation, and the result before changing it.
*, *::before, *::after {
box-sizing: border-box;
}
.notice {
width: min(100%, 42rem);
padding: 1.25rem;
border: 2px solid #10231f;
margin-inline: auto;
}Working rules
- Set border-box globally
- Use padding for internal rhythm and gap for sibling rhythm
- Inspect overflow instead of hiding it immediately
A common mistake
Fixed widths plus padding can overflow a narrow viewport when the content-box default is assumed.
Guided practice
Calculate what a 300px content-box with 20px padding and 2px borders occupies.
- Copy the example into the editable notebook below.
- Change one thing at a time and keep the program or rule valid.
- Explain the result in one sentence without relying on jargon.