tags:

views:

1072

answers:

3

So, I am creating a site with DIV's. Everythings working out except when I create a DIV, in my css file I create them like this (example):

newdiv { width: 200px; height: 60px; padding-left: 20px; text-align: left; }

NOW, when I add the padding-left property, the width of the DIV changes to 220px, and I want it to remain at 200px;

Let's say I create another DIV named anotherdiv exactly the same as newdiv, and put it inside of newdiv but newdiv has no padding and anotherdiv has padding-left: 20px. I get the same thing, newdiv's width will be 220px;

How can I fix this problem?

+2  A: 

Put a div in your newdiv with width: auto; and margin-left: 20px;

Remove the padding from newdiv.

The W3 Box model page has good info.

rvarcher
+2  A: 

This is the famous Internet Explorer box model bug where IE will wrongly add the padding on to the dimensions of an element with a fixed size.

There are many work-arounds (most notably the box-model hack), but (mostly for the sake of not using 'hacks') I usually end up avoiding padding on any element that has a fixed size and applying margin to a child element instead.

da5id
No, it's not the box model bug. The box model but doesn't work the way that you describe it, but the other way around. When in effect, IE does NOT add the padding to the dimensions.
Guffa
Sorry Guffa but that's incorrect. According to CSS spec, padding shouldn't be taken into account when (the browser is) calculating the dimensions of an element. Margin on the other hand...
da5id
No, according to spec the padding SHOULD be taken into account when calculating the dimensions. Read the wikipedia page that you linked to...
Guffa
Am I the only person in the world that hates how box model works? I mean come on... padding should not change the element size... it should be inner. Everytime you add some padding to a box, you have to do the math to rest the value to the total width, it's annoying. And I know I could use another div inside and add the padding there, but's that's just more annoying...
emzero
A: 

when I add the padding-left property, the width of the DIV changes to 220px

Yes, that is exactly according to the standards. That's how it's supposed to work.

Let's say I create another DIV named anotherdiv exactly the same as newdiv, and put it inside of newdiv but newdiv has no padding and anotherdiv has padding-left: 20px. I get the same thing, newdiv's width will be 220px;

No, newdiv will remain 200px wide.

Guffa
Guffa, that's not how the standards are supposed to work at all. Padding is definitely not supposed to affect the dimensions of the element to which it's being applied. With the greatest respect, is it possible you're confusing 'padding' with 'margin'?
da5id
Yes, the padding is definitely supposed to affect the dimensions of the element. I think that it's you who is confused... how do you propose that the margin would affect the dimensions of the element?
Guffa
Actually, you know I think we're both right in a way. I'm so used to coping with the effects that I'd entirely forgotten the standard. I found a good explanation here http://www.quirksmode.org/css/box.html
da5id
So, my apologies Mr Guffa. But for practical purposes (which is after all what we're talking about) my advice is still good no?
da5id
Well, you are right so far as that there is a box model bug, but it doesn't apply to this question... :)
Guffa
Heh, da5id /really/ got his stuff mixed up on this one. :)
Hexagon Theory