I learned something about percentage-based (%) padding
today that I had totally wrong in my head! I always thought that percentage padding was based on the element itself. So if an element is 1,000 pixels wide with padding-top: 50%
, that padding is 500 pixels. It's weird having top padding based on width, but that's how it works — but only sorta. The 50% is based on the parent element's width, not itself. That's the part that confused me.
The only time I've ever messed with percentage padding is to do the aspect ratio boxes trick. The element is of fluid width, but you want to maintain an aspect ratio, hence the percentage padding trick. In that scenario, the width of the element is the width of the parent, so my incorrect mental model just never mattered.
It doesn't take much to prove it:
See the Pen
% padding is based on parent, not itself by Chris Coyier (@chriscoyier)
on CodePen.
Thanks to Cameron Clark for the email about this:
The difference is moot when you have a block element that expands to fill the width of its parent completely, as in every example used in this article. But when you set any width on the element other than 100%, this misunderstanding will lead to baffling results.
They pointed to this article by Aayush Arora who claims that only 1% of developers knew this.
The post Oh Hey, Padding Percentage is Based on the Parent Element’s Width appeared first on CSS-Tricks.
from CSS-Tricks https://ift.tt/34XFg1H
via IFTTT
No comments:
Post a Comment