The box model
5 minEvery element on a page is a box made of four layers. Learn how CSS adds them up, and your builds stop coming out bigger than the design.
Lesson video
Tastebud · 5:07 · Captions on screen
Size is content, padding and border added up, and box-sizing decides whether width counts all three.
- Why. CSS puts width on the content alone for most elements and adds padding and border on top. Figma counts padding inside the frame, so copied numbers come out bigger.
- Real example. Select Follow on an artist page and select it again to stop. A two-state button often needs two labels, so let padding around the text set the pill's width. (Spotify, Nov 2026)
- When to break it. Fix both sizes when the shape is the point. A round, icon-only play button needs equal width and height, or the circle turns into an oval.
- Common mistake. Copying a card's W 320 from Figma into width: 320px. With 24px of padding and a 1px border it renders 370 wide, past the edge of a 360px phone screen.
Our take. Let padding and the label set a component's size, and leave widths to the layout around it. Copying Figma's W and H into width and height is the bug, whichever box-sizing you use.
box-sizing decides what width means
With the default, content-box, width covers the content only, and padding and border get added on top. With border-box, width covers all three, the way Figma measures a frame. Buttons are the exception: browsers give them border-box already.
A card set to 320px renders 370px wide by default
- content-box (the default): 370 px. 320 for the content, plus 24 and 24 of padding, plus 1 and 1 of border.
- border-box: 320 px. 320 in total. The content gets what's left: 270.
Padding grows the tap area, margin doesn't
Taps land on the content, padding and border, and stop at the border. To make a small button easier to hit, add padding. iPhone wants 44 by 44 points, Android 48 by 48 dp, WCAG 2.2 AA at least 24 by 24 pixels.
- Sits outside the border
- No background, and taps there miss
- Spaces boxes apart
- Sits inside the border
- The background fills it, and taps count
- Grows the tap target, icon stays small
Let the label set the width
A Follow pill given a fixed width from a design file can't grow, so the longer label, Following, crowds its right edge. With no width, and min-height in place of height, the padding and label set the size.
- width: 88px; height: 32px;
- Following crowds the right edge, since the width can't grow
- The 88 already includes padding, because buttons use border-box
- min-height: 32px; and no width at all
- 16px of padding on each side of the label sets the width
- Follow and Following each get a pill that fits
- It stays at least 32px tall
Try it
Drag padding and margin, watch the total
This box is a card designed at 320px wide. Drag the controls and watch the total width it takes, margin included.
GoalFit the card and its margin inside a 360px phone screen, keeping 24px of padding.
- margin 16px
- border 1px
- padding 24px
- content 320px
Total width 402px = 320 content + 2×24 padding + 2×1 border + 2×16 margin
widthpaddingborder-widthmarginbox-sizing.card {
width: 320px;
padding: 24px;
border-width: 1px;
margin: 16px;
box-sizing: content-box;
border-style: solid;
}What to notice: In content-box the card takes 320, plus 48 of padding, plus 2 of border, plus 32 of margin: 402px, too wide for the screen. Switch to border-box and the card is 320, or 352 with its margin. Margin never changes the card itself.
Fix the Follow button
A Follow pill like the one on Spotify's artist pages. The design says it's 32px tall and as wide as its label. Press it to switch to Following, read the rendered size, then fix the CSS.
Tab adds two spaces. Press Esc, then Tab, to leave the editor.
- No fixed width: the label decides it (not yet)
- min-height: 32px instead of a fixed height (not yet)
What to notice: Watch the Rendered line. With the fixed width it stays 88 wide, and Following crowds the pill's right edge. Once fixed, the pill stays 32 tall and grows with the label. Buttons already use border-box, so the 88 included the padding.
Quick check
Figma shows a card at W 320 with 24px padding and a 1px border. Your build, with width: 320px and the default box-sizing, renders 370px wide. What's the best fix?
Watch next
Watch it for What border-box changes about width and padding, compared with the default.
Watch it for Content, padding, border and margin one at a time, and how each one changes an element's size.
Connects to: 2.1 How browsers draw a page · 12.2 Auto layout to flexbox and grid · 14.2 Touch targets and safe areas · 7.5 Spacing, type and colour scales
Sources · checked Nov 2026
- MDN: The box modelContent, padding, border and margin; in the standard model width sets the content box and padding and border are added; in the alternative model width is the visible box; margin isn't counted in the box's size and the box stops at the border; the background shows behind padding
- MDN: box-sizingcontent-box is the default and excludes padding and border; border-box includes them; Baseline widely available since July 2015
- HTML Living Standard: 15.3.10 Form controls (rendering)The default style sheet gives button, select and button-type inputs box-sizing: border-box
- Figma Help: Guide to auto layoutHug contents, Fill container and Fixed width / height; padding sits inside the frame (a 40px text with 10px padding each side gives a 60px frame)
- Spotify Support: Follow artists, podcasts, and showsAn artist's profile has a Follow button; select it to follow, and select it again to stop following
- Spotify web player: Arijit Singh's artist pageThe page's wording: a small outlined Follow button and a Popular list (the video's screen)
- Apple Human Interface Guidelines: ButtonsA button needs a hit region of at least 44x44 pt
- Android Developers: Make apps more accessibleTouch targets of at least 48 x 48 dp
- W3C: WCAG 2.2, 2.5.8 Target Size (Minimum)Level AA: targets at least 24 by 24 CSS pixels, with exceptions such as enough spacing