日本語

CSS padding and margin in % also resolve against the parent’s width

​​CSS​CSS Grid,Flexbox,Playwright

When you write CSS padding or margin as a percentage, the top and bottom values are calculated from the width of the parent (the containing block), just like the left and right values. The parent's height has no effect. This can be useful for spacing that scales with the screen width, or for boxes that keep an image's or card's aspect ratio. But if you write margin-top: 50% to center a box vertically, the box will not end up where you expect.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I used a 800px wide by 600px high viewport, set margin to 0 on body, and measured the computed padding and margin with getComputedStyle(), and the position and size of boxes with getBoundingClientRect(). I did not test Firefox, Safari, or mobile browsers.

Top and bottom padding are also a percentage of the parent's width

I placed .box inside a parent 400px wide and 100px high, and wrote padding: 10%. The computed padding was 40px on all four sides: top, right, bottom, and left. That is 10% of the parent's width of 400px. It is not 10% of the height of 100px, which would be 10px.

css
.parent { width: 400px; height: 100px; }
.box { padding: 10%; }

Next, I wrote only padding-top: 10% on .box and measured while changing the parent's width and height one at a time.

Parent size padding-top of .box
Width 200px, height 100px 20px
Width 400px, height 100px 40px
Width 800px, height 100px 80px
Width 400px, height 50px 40px
Width 400px, height 300px 40px

When I changed the parent's width, padding-top changed in the same proportion. When I changed the height, it stayed at 40px.

Three parent boxes stacked vertically. In the parent that is 300px wide and 120px high, the top padding (light blue) is thin. In the parent that is 600px wide and 120px high, the top padding is twice as thick. In the parent that is 300px wide and 240px high, the parent is taller, but the top padding is as thin as in the first one.
A box with padding-top: 10% placed in three parents of different sizes. Light blue is the padding area. Doubling the parent’s width doubles the padding, and doubling its height changes nothing.

The MDN page for padding explains that percentage values are relative to the containing block's inline size, which means the width in horizontal writing. The CSS 2 specification's section on padding also states explicitly that the width is the base for padding-top and padding-bottom.

margin-top also uses the width as its base

The same applies to margin. I placed .box with margin-top: 10% inside a parent 400px wide and 100px high, with display: flow-root set on the parent. The computed value was 40px, and the top edge of .box moved 40px down from the top edge of the parent. The MDN page for margin also states that percentage margins are relative to the containing block's inline size, which is the width in horizontal writing. The CSS 2 specification's section on margin also notes that margin-top and margin-bottom work the same way.

margin-top: 50% does not center a box vertically

If you write margin-top: 50% to center a box vertically, the base is the parent's width, so the box is pushed further off as the parent gets wider. In a parent 800px wide and 300px high, I wrote margin-top: 50% on a .box 50px high. The margin-top was 400px. The top edge of .box ended up 400px below the top edge of the parent, which is outside the parent's 300px height.

In the same parent, I wrote position: relative; top: 50% on .box, and it moved 150px down from the top edge of the parent. That is half of the parent's height of 300px. As described in the MDN explanation of the containing block, percentages for top, bottom, and height are relative to the height of the containing block, while percentages for left, right, width, padding, and margin are relative to its width. margin and padding use the width even for their top and bottom values.

css
.parent { width: 800px; height: 300px; } /* For the margin-top test, I also set display: flow-root */

/* 50% of the parent's width (800px) gives margin-top: 400px */
.box-margin { height: 50px; margin-top: 50%; }

/* 50% of the parent's height (300px) moves it down 150px */
.box-top { height: 50px; position: relative; top: 50%; }
Two cases stacked vertically. In the top case, "margin-top: 50%", a pink box appears below a dotted parent that is 600px wide and 200px high, well away from the parent. In the bottom case, "position: relative; top: 50%", the top edge of a light blue box lines up with the vertical middle of the parent.
A 40px high box placed inside a parent 600px wide and 200px high. margin-top: 50% uses half the parent’s width, which is 300px. top: 50% uses half the parent’s height, which is 100px.

top: 50% moves the top edge of the box, so the middle of the 50px high box is at 175px from the top edge of the parent. That is 25px lower than the middle of the parent (150px).

Making a box whose height follows its width

Because top and bottom padding are determined by the width, you can build a box whose height changes with its width. If you set height: 0 and write padding-top: 100%, the box's height equals its width. When the parent was 200px wide, the box was a 200px by 200px square. When the parent was 400px wide, it was a 400px by 400px square.

css
.square {
  height: 0;
  padding-top: 100%;
}

You can also specify the aspect ratio with the aspect-ratio property. A box with only aspect-ratio: 1 / 1 and no padding was 200px by 200px when the parent was 200px wide, and 400px by 400px when the parent was 400px wide.

Which width is the "parent width"?

Strictly speaking, the base for percentages is the width of the containing block. In most cases, this is the content width of the immediate outer element, but it changes depending on how the element is placed. I measured padding-top: 10% in each of the following placements.

Placement Width used as the base padding-top
Item 200px wide inside a flex container 600px wide Container width of 600px 60px
Item in column 1 of a 600px wide grid (columns of 200px and 400px) Column width of 200px 20px
Item in column 2 of the same grid Column width of 400px 40px
Normal element inside a position: relative parent with width 400px and padding: 50px Parent's content width of 400px 40px
position: absolute element inside the same parent Parent's width including padding, 500px 50px

For flexbox items, the base was the width of the container, not the item's own width (200px). The Flexbox specification states that percentage margin and padding on flex items are also relative to the inline size of the containing block, the same as for block boxes.

For grid items, the base was the width of the column the item is placed in, not the width of the whole grid (600px). The Grid specification defines the containing block of a grid item as the grid area where the item is placed. Even with the same padding-top: 10%, the spacing differs when the columns have different widths.

For an element with position: absolute, the base was the parent's width including its padding (400px plus 50px on each side). This matches the MDN explanation of the containing block, which says that the containing block of an absolutely positioned element is formed by the padding box edges of the nearest ancestor whose position is not static.

In vertical writing, the height is the base

It is easy to remember the base as "width," but the specification says "inline size of the containing block." The inline size is the length in the direction in which text runs. If you set writing-mode: vertical-rl on the parent to use vertical writing, text runs from top to bottom, so the inline size is the height.

I placed .box with padding: 10% inside a vertical-writing parent 400px wide and 200px high. The padding on all four sides was 20px. That is 10% of the parent's height of 200px. The width of 400px was not used as the base, which is the opposite of horizontal writing. The CSS Writing Modes specification states that percentages for margin and padding, which CSS 2.1 resolved against the width, are resolved against the inline size in CSS3.

​Posted by 86