In this series, I try to focus on CSS weird parts, throw quiz and its answer, hoping to give a better understanding of how CSS works in depth.
The problem:
Having the following code snippet, for two divs, the #container
div has a width of 300px and hight of 200px, the #box
div has 100% padding-left and padding-top.
what is the rendered #box
div width and height respectively?
<div id="container" style="width: 300px; height: 200px;">
<div id="box" style="padding-top: 100%; padding-left: 100%;"></div>
</div>
Options:
- 200px and 200px
- 300px and 300px
- 200px and 300px
- 300px and 200px
The answer:
you can find the answer below, but please give yourself a couple of minutes to think about it, to make sure you benefit from this quiz.
The correct answer is: "300px and 300px"
ππ
If you wonder why this is the correct answer, please read the discussions below.
Top comments (15)
To know why this is the right answer we have to dig into how the CSS percentage unit works, the percentage unit is a CSS relative unit so it has to be relative to an absolute value, and this is summary of how it works with different properties:
So here we are working with padding top and left properties, and both are relative to the
#container
's width and be calculated based on its value.Your explanation is very straightforward and convincing... I will definitely fiddle with this. Thanks so much. @Abdelrahman . :)
You're welcome, I'm glad that you benefited from this. ππ
You can find more about this here ππ
CSS percentage unit, the evil parts
Abdelrahman Ismail
I like your posts, cuz it helps me think CSS more deeply..I am wondering why 300px Γ 300px...I think if you should explain and give reasons for your answers..that way we can learn better and also get the concept..
Thank you, sure I'll explain why. ππ
I know that padding is always calculated from the width but I can't say why, there is no real reason in the specs.
I just knew the answer because I had to build a webpage with squares as page elements where I used the padding technique to get it to work without fixed widhts and heights for the squares.
This is one of the CSS behaviors that can be awkward at first, but once you learn it, you will know how it's so powerful tool.
As you said you can use it to create perfect squares, even more you can create a fixed ratio between width and height for example, this is how you can create a responsive video base on this behavior:
This was always a useful trick for preserving an element's aspect ratio (change the percentage of the padding), though I haven't used CSS in a few years now so not sure if there are better ways now, but this still should work flawlessly.
Wrong answer: I choose 300px and 200px.
I thing it's the correct because box have no content.
Please explain why
Yes, this the behavior for percentage unite with
padding
property (even top and bottom padding) it always calculated relative to element's parent width.Because padding is always calculated from the width.
i am also agree with @Camilo's answer.
I'm afraid that it's not a valid answer; however, you can check this fiddle: jsfiddle.net/6eLwtm9v/