Skip to content

Instantly share code, notes, and snippets.

@humbleRumble
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save humbleRumble/4d6ee913cd0e7a9841bb to your computer and use it in GitHub Desktop.
Save humbleRumble/4d6ee913cd0e7a9841bb to your computer and use it in GitHub Desktop.

What is the CSS padding trick?

First some information about containing blocks for reference.

9.1.2 Containing blocks
In general, generated boxes act as containing blocks for descendant boxes; we say that a box "establishes" the containing block for its descendants. The phrase "a box's containing block" means "the containing block in which the box lives," not the one it generates.

Now, here's the trick.

Padding

  • Percentages: refer to width of containing block

It is intuitive that horizontal padding percentages would reference the width of the containing block. However, vertical padding percentages also refer to the width, not the height

What does that mean for maintaining aspect ratio?

What this means is that if you have an element which is the same width as its parent, and you set padding-bottom: 50%, the element will be half as wide as it is tall.

<div style="padding-bottom: 50%;"></div>

However, if the element is less than the full width of its parent, the numbers change.

For example, if the element is half the width of its parent and you set padding-bottom: 50%, it will be as tall as it is wide.

<div style="width: 50%; padding-bottom: 50%;"></div>

This is because percentages for the padding property are relative to the containing block, not the element.

To make an element which is half its parent's width retain an aspect ratio of 2:1 (half as tall as it is wide), cut the percentage in half.

<div style="width: 50%; padding-bottom: 25%;"></div>

How can I use this in the real world?

If the element in question contains any content which takes up space in the document flow, the height will change. To avoid this you can take advantage of another CSS trick.

10.1 Definition of "containing block"
4. If the element has 'position: absolute', the containing block is established by the nearest ancestor with a 'position' of 'absolute', 'relative' or 'fixed', in the following way:

  • In the case that the ancestor is an inline element, the containing block is the bounding box around the padding boxes of the first and the last inline boxes generated for that element. In CSS 2.1, if the inline element is split across multiple lines, the containing block is undefined.
  • Otherwise, the containing block is formed by the padding edge of the ancestor.

When an element is set to position: absolute it will be positioned in reference to it's closest ancestor with position: relative. If the element has no such ancestors, it will positioned in reference to the root element.

This means, if you set position: relative on this new responsive div, you can insert a child with position: absolute and tell it to be the same size as the parent using top: 0; width: 100%; bottom: 0;.

To make sure the contents won't overflow the boundaries set overflow: auto

<div style="position: absolute; top: 0; width: 100%; bottom: 0; overflow: auto;"></div>

This will create an element which retains its aspect ratio and can contain content without changing the aspect ratio.

<div style="padding-bottom: 50%; position: relative;">
    <div style="position: absolute; top: 0; width: 100%; bottom: 0; overflow: auto;">
        Content...
    </div>
</div>

See it in action!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment