Quick CSS Quiz #2

Abdelrahman Ismail on October 01, 2018

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. ... [Read Full]
markdown guide
 

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:

  • margin, padding, left, right
    • It will be relative to element's parent width (even top and bottom margin/padding will be relative to the parent’s width)
  • top, bottom
    • It will be relative to element's parent height
  • transform
    • It will be relative to the element itself, either its width or its height.

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. πŸ˜„πŸ˜„

 

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:

.videoWrapper {
    position: relative;
    padding-bottom: 56.25%; /* 16:9 */
    padding-top: 25px;
    height: 0;
}
.videoWrapper video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
 

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.

 
 
[deleted]
 

I'm afraid that it's not a valid answer; however, you can check this fiddle: jsfiddle.net/6eLwtm9v/

code of conduct - report abuse