Measure what changes when a CSS box uses border-box sizing
Admin ·
Decide which box your width must describe before adjusting the spacing. Content-box starts with the content dimensions. Border-box counts padding and borders inside the stated width and height when they fit.
Spacing and Padding Visualizer
Measure the starting content box
Select Reset example. The declared width and height are 136 × 72 px. Padding is 10 px on top and bottom and 16 px on the sides. The border is 3 px on each side.
In content-box mode, the border box measures 174 × 98 px. The colored margin adds 18 px to each side and 12 px to the top and bottom, giving 210 × 122 px with margins.
Keep the declared size and switch sizing
Set Box sizing to border-box. The border box is now 136 × 72 px and the content box is 98 × 46 px. The widths and heights you typed stayed the same.
Enter 4 8 12 16 for Padding. The preview shows top 4 px, right 8 px, bottom 12 px and left 16 px. The content becomes 106 × 50 px while the border box stays 136 × 72 px.
padding: 4px 8px 12px 16px;W3C CSS Box Model Level 3W3C CSS Box Model padding shorthandW3C CSS Basic UI box-sizing
Verify which dimensions the exported rule controls
Download spacing-box.css and add its class to an element. Read its border-box size and computed padding in the browser's inspector. Keep box-sizing with the rule when copying those dimensions into a layout.
The preview isolates a single box, so its top and bottom margins remain visible. Neighboring block margins can collapse in other layouts. Check the resulting gap between real elements if that gap is the size you need.
W3C CSS Basic UI box-sizingW3C CSS 2 adjoining margins and collapsing
Common questions
If I type three values, how is the left side chosen?
Three values set top, horizontal and bottom spacing. The second value applies to both right and left.
Which shaded layer stays outside the border?
Margin stays outside the border. Padding lies between the border and content.
Sources
Put it into practice.
- Spacing and Padding Visualizer
- Spacing and Padding Visualizer
- CSS Grid Generator
- CSS Flexbox Playground