How do percentage values get their reference size in CSS?
Quick answer
A CSS percentage does not always refer to an element’s width or height. Its reference depends on the property: font-size: 125% uses the parent’s font size, while background-position: 50% aligns the image’s midpoint with the positioning area’s midpoint. Some properties, such as border-width, do not accept percentages. Check the specific property before calculating. [S1] [S2]
The question, the details, the next step.
A practical checklist
Write down the exact CSS property using the percentage.
Check whether that property accepts percentages.
Identify its reference, such as the parent’s font size, the parent’s width, or the background-positioning difference.
Work through a small numeric example using known dimensions or computed values.
Check the result in the relevant layout without assuming another property uses the same rule.
A few more useful answers
Does `50%` always mean half of the parent’s size?
Can I use a percentage for `border-width`?
No. border-width accepts lengths rather than percentages, so a percentage is not a valid border thickness for that property. [S1]
What should I check when a percentage produces an unexpected result?
Start with the property name, confirm its accepted value types, and find the reference used for that property. If the calculation depends on sizes, verify those sizes rather than assuming the element’s width is the reference.
Sources
References used for this guide.
- [S1] MDN: CSS values, units, and percentage references developer.mozilla.org · Checked
- [S2] MDN: Percentage positioning for CSS backgrounds developer.mozilla.org · Checked
