vertical percentage margins

some experiments and backgrounds

In his article Vertical Percentages in CSS (November 14th, 2013) Louis Lazaris writes:

My attention to this article was drawn by the topic "This Week in CSS - November 25 2013" in the SitePoint forums.

1. The tests

test 1

#inside

On the right is a green bordered #containingBlock with a width of 200px and a height of 100px.
Inside is an #inside container with a {margin-top: 10%}.
Now 10% of the height is: 10px. And 10% of the width is: 20px.
For the margin-top the real computed value in this browser *) is: .
!

*) "this browser" = your actual browser while visiting this page.

test 2

#inside

On the right is a green bordered #containingBlock with a width of 200px and a height of 150px.
Inside is an #inside container with a {margin-top: 10%}.
Now 10% of the height is: 15px. And 10% of the width is still: 20px.
For the margin-top the real computed value in this browser is: .
!

test 3

#inside

On the right is a green bordered #containingBlock with a width of 400px and a height of 100px.
Inside is an #inside container with a {margin-top: 10%}.
Now 10% of the height is: 10px. And 10% of the width is: 40px.
For the margin-top the real computed value in this browser is: .
!

test 4

#inside

On the right is a green bordered #containingBlock with a width of 200px, and a height of 100px.
Inside is an #inside container with a {margin-top: 10%} and a {margin-left: 10%}.
Now 10% of the height is: 10px. And 10% of the width is: 20px.
For the margin-top the real computed value in this browser is: .
!
For the margin-left the real computed value in this browser is: .
As expected: !

test 5

#inside

On the right is a green bordered #containingBlock with a width of 80px, a height of 100px, a padding-left of 20px and a padding-right of 100px.
Inside is an #inside container with a {margin-top: 10%} and {margin-right: -100px}.
Now 10% of the height is: 10px. And 10% of the width is: 8px.
For the margin-top the real computed value in this browser is: .
!

test 6

#inside  more inside  last inside

On the right is a green bordered #containingBlock with a height of 100px, but without a width.
Inside is an #inside container with a {margin-top: 10%}.
Now 10% of the height is: 10px. And 10% of the computed width is: 10% of , or .
For the margin-top the real computed value in this browser is: .

This is according to the css3 rules!

test 7

#inside
more inside
more inside
no more inside

On the right is a green bordered #containingBlock with a width of 200px, but without a height.
Inside is an #inside container with a {margin: 3%} around.
The #inside container has also a {padding: 7%} around.
The 3% of the width is: 6px.
For all margins the real computed value in this browser is: .
!
The 7% of the width is: 14px.
For all vertical paddings the real computed value in this browser is: , like for the horizontal paddings.
!

2. Browser differences

Firefox 23/25: everything well done.

Chrome 31: everything well done.

Opera 17/18: everything well done.

Safari-Win (5.1.7): everything well done;
with a difference in the rounding of the computed values: floating point numbers are rounded downwards (e.g. 19.9px → 19px), while all other browsers are rounding upwards (19.9px → 20px). - In the test-6 results I accepted both variants.

Internet Explorers:

3. The specs

the css2.1 specification about percentage margin properties:

Source: w3.org/TR/CSS2/box.html#margin-properties.

the css3 specification about percentage margin properties:

Source: dev.w3.org/csswg/css-box/#the-margin-properties (Editor's Draft 12 October 2013).

Note: 'vw' and 'vh' mean 'viewport width' and 'viewport height'; '100vw'=100% of the viewport width, and '100vh'=100% of the viewport height; see viewport-descriptors: "Viewport lengths (vw, vh, vmin, vmax) are relative to the initial viewport."

the css3 specification about 'horizontal' and 'vertical':

Horizontal: ltr=left-to-right reading (or rtl=right-to-left, or bidi=bidirectional), paragraphs grow downwards.

Vertical: in other language systems the reading direction is top/bottom (or bottom/top) and the block flow direction is to the left or to the right: 90 or 270 degrees rotated.

Sources: w3.org/TR/2012/WD-css3-writing-modes-20121115 and dev.w3.org/csswg/css-box/#writing-mode.

the differences in css2.1 and css3:

In css2.1 the vertical percentages are undefined if the containing box has no width property. In css3 is added that then the vertical percentages are based on the computed width of the content of the box.

In css2.1 always the width is the leading dimension for the computing of vertical percentages. In css3 is added that it must be the height in case of 'vertical languages'.

4. What I understand of it

The w3c has as axioma:

The same principle is working for the paddings, as test 10 is illustrating.

In the 'horizontal languages' we are used to think in columns of text, reading from left to right (or rtl, or mixed); but always top/down. In order to get readable lines, the text (or: text-columns) must not exceed a certain line length. See for instance: Readability: the Optimal Line Length.

In the 'vertical languages' the reading direction is 90 or 270 degrees rotated.

Note: All this apart from the usual problems when trying to get relative heights!

Francky Kleyneman
Nov. 30, 2013