Why does an element’s percentage width look larger than expected?
Quick answer
A CSS percentage width is relative to the element’s containing block, not necessarily the page or the ancestor you had in mind. [S1] Identify that reference and calculate its width multiplied by the percentage. If the result still looks too large, compare like-for-like measurements and check padding, borders, and box-sizing: these can affect whether the width refers to the content area or border area. [S1]
The question, the details, the next step.
A practical checklist
Select the element with the unexpected width.
Identify its containing block rather than assuming the page or a nearby wrapper is the reference.
Record the reference width and calculate the percentage result.
Compare like-for-like content or outside measurements.
Check padding, borders, and box-sizing if the measurements differ.
A few more useful answers
Does `width: 80%` always mean 80% of the nearest parent?
It means 80% of the element’s containing block, which may not match the ancestor you assumed. Identify the reference for the specific layout before calculating. [S1]
Why can the visible box be wider than the calculated percentage width?
The declared width and the outside measurement may describe different parts of the box. Check padding, borders, and box-sizing, which can affect whether width refers to the content area or border area. [S1]
Does the 400px example explain every CSS layout?
No. It illustrates the calculation under the stated assumption that the parent’s relevant width is 400px. It does not establish which containing block applies in every layout; identify that reference in your own case.
Sources
References used for this guide.
- [S1] The CSS width property: Percentage values developer.mozilla.org · Checked
