Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

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]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I keep seeing percentages behave differently in CSS. Is there one general rule for what they’re a percentage of?

Geez

The useful general rule is that the property determines what a percentage refers to. There is no universal “percentage of the parent’s width” rule. For instance, font-size percentages use the parent’s font size, while width percentages use the parent’s width. Some properties do not accept percentage values at all. [S1]

When you see a percentage, ask two questions: “Does this property allow percentages?” and “What reference does this property use?” The answer depends on the property, not simply on where the element sits.

Follow-up question

How does that work for typography? If I set a child’s font size to 125%, what number should I expect?

Geez

For font-size, calculate the percentage from the parent’s font size. As a simple example, if the parent is 20px and the child has font-size: 125%, the child’s font size is 25px: 20 × 1.25. The parent size is the reference, not the child element’s width. [S1]

You can make the relationship visible in a small example:

.parent {
  font-size: 20px;
}

.child {
  font-size: 125%;
}

This calculation assumes the parent’s computed font size is 20px. If you do not know that value, inspect the parent’s computed styles rather than guessing. This example explains font-size; it does not establish a rule for every typography-related property.

Follow-up question

What about borders? I tried a percentage for border width and did not get the result I expected.

Geez

border-width accepts lengths rather than percentages, so there is no percentage reference size to calculate for it. [S1]

For example, this is not a valid way to request a border whose thickness is a fraction of the parent’s width:

.box {
  border-width: 2%;
}

If you want a particular border thickness, choose a supported length value and check the result in your layout. The important distinction is between a percentage using an unexpected reference and a property that does not allow percentages. This point applies to border-width; it does not establish the rules for every border-related property.

Follow-up question

I’ve also seen background-position: 50%. Does that mean the image is placed 50% across the container?

Geez

Not quite. For background-position, the percentage is applied to the difference between the background positioning area and the image’s size along that axis. At 50%, the image’s midpoint aligns with the positioning area’s midpoint; it is not simply an offset equal to half the area’s size. [S2]

Suppose, for illustration, the area is 300px wide and the positioned image is 200px wide. The difference is 100px. At 50%, the offset is half that difference, or 50px, so the image’s center lines up with the area’s center. The same idea applies along the other axis. This example assumes those dimensions and does not establish how large an image will be in a real layout.

Here’s a compact property-reference guide:

  • font-size: 125% — percentage of the parent’s font size. [S1]
  • width: 50% — percentage of the parent’s width. [S1]
  • border-width: 2% — percentages are not accepted for this property. [S1]
  • background-position: 50% — aligns the image’s midpoint with the positioning area’s midpoint on that axis. [S2]

When a result surprises you, identify the exact property, then check its accepted value types and percentage reference. These examples cover only the named properties; they do not establish percentage rules for every CSS property.

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?

No. The property determines the reference. For example, font-size uses the parent’s font size, while background-position uses the difference between the positioning area and image size on the relevant axis. [S1] [S2]

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.

  1. [S1] MDN: CSS values, units, and percentage references
    developer.mozilla.org · Checked
  2. [S2] MDN: Percentage positioning for CSS backgrounds
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work