Definitely a candidate for one of my more recent WTF moments. I recently discovered, and subsequently had to work around, the following CSS behaviour: margin/padding-top/bottom percentages are calculated by reference to the width of the containing block, not the height.

This is at the very least slightly confusion especially as CSS enables you to calculate top/bottom with respect to height as expected. I’ve not found an especially clear explanation for this, however most opinions converge on the following,

  • We tend to set/deal with widths, not heights explicitly,
  • It makes rendering that much easier for “horizontal flow,” as discussed here,
  • There has to be some shared reference between margin-top, margin-left etc. as setting margin:5% is a thing.

The end result being that setting resizeable aspect ratios are a breeze as we have constant referring to the width of the element in question. Others, much braver than myself, have looked in to how to explicitly set something equivalent to top/bottom-margin. However, I would advise against this as it exploits the quirks of the CSS specification, which is never a good place to be.

All the best,

Tom


Tom Martin

Data scientist, London, UK