Mobile app version of vmapp.org
Login or Join
Alves908

: "Effective width" of a css element could reasonably includes what? width, ?margin?, ?padding? Eric Meyer just tweeted that he got 100% in this quiz: http://net.tutsplus.com/quizzes/nettuts-quiz-1-beginner-css/

@Alves908

Posted in: #Css

Eric Meyer just tweeted that he got 100% in this quiz:
net.tutsplus.com/quizzes/nettuts-quiz-1-beginner-css/
So I took the quiz, and at the end they told me that I got one question wrong:


What’s the effective width of the
container?
#container {width: 100px; padding: 10px; margin: 20px;}


To which I answered: 20px + 100px + 20px = 140px

After a lot of work, I unraveled that the quiz's suggested "correct" answer was 120px.

Could someone explain why 120px would be a more appropriate answer?

Edit: Created a jsfiddle of the situation, and it appears that (for example) inline-block elements with padding incorporate the padding into the width, which I somehow managed to go all this time without realizing.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Alves908

2 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

So the breakdown is thus:

The margin isn't being counted in the question since it wouldn't contribute anything visible to the element (which I chalk up to the vagueness of the question itself).

In addition, padding is counted in the visible width, which I have somehow managed to not get over all these years. Stupid box model.

As a result, the visible width if a background is applied would be:
10px (left padding) + 100px (width) + 10px (right padding) = 120px.

Here is a jsfiddle of that situation:
jsfiddle.net/tchalvakspam/XC2Br

10% popularity Vote Up Vote Down


 

@Candy875

Padding is space inside the object, margin is space outside the object. The width property is how wide the stuff inside the container is. As the effective width here means how much space the object takes up the margin doesn't count towards the width of the object.

If two elements are next to each other the margins may collapse, which is fine because they aren't really part of the object.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme