tags:

views:

324

answers:

7

I was very surprised when I found that a <div> with a size of - say - 200px becomes 220px wide if you give it 10px padding. It just makes no sense to me, the external size should not change when an internal setting does. It forces you to adjust the size every time you tweak the padding.

Am I doing something wrong, or is there a reason for this behavior?

EDIT: I know this is how it's supposed to work, my question is why? Is it logical in a way I don't understand? Does this give any advantage over the opposite approach of keeping size and padding separate?

+4  A: 

Padding is supposed to be in addition to the given width of an object.

See the CSS 2.1 specification for box model.

While it is true that you can view padding as either an internal or an external attribute, the fact of the matter is that according to the current specifications it is an external attribute. It was a choice between two, as far as I can tell, equally valid options.

I haven't read up on the box-model attribute, but assuming that alex is right, then in the future you will be able to choose between the two ways of interpreting padding.

Sean Vieira
Isn't `margin` supposed to be the external attribute? `padding` doesn't do anything to the external part of the element.
kemp
@kemp; if you look at the box model in the specs, you will see that padding is internal (i.e. inside the border) and margin is external (outside the border.) My best guess as to why they chose to do that rather than have padding subtract from the element's width and hight is can be summed up in one word,`images`.
Sean Vieira
And spelling apparently, is not my forté
Sean Vieira
+1  A: 

If the size increases with padding, it's working as intended. In browsers with broken box models like older Internet Explorer versions, the div will be 100 pixels wide, but that's incorrect handling of the CSS.

http://www.w3schools.com/css/css_boxmodel.asp

Sheep Slapper
A: 

yes, there is a reason - the css box model specification.

http://www.w3.org/TR/CSS2/box.html

http://css-tricks.com/the-css-box-model/

zalew
+5  A: 

There are two different so-called "box models", one adds the padding (and border) to the specified width, while the other does not. With the advent of CSS3, you can luckily switch between the two models. More precisely, the behaviour you are looking for can be achieved by specifying

box-sizing: border-box;
ms-box-sizing: border-box;
webkit-box-sizing: border-box;
moz-box-sizing: border-box;
width: 200px;

in your div's CSS. Then, in modern browsers, the div will always stay 200 px wide no matter what. For further details and a list of supported browsers, see this guide.

Edit: WRT your edit as to why the traditional box model is as it is, Wikipedia actually offers some insight:

Before HTML 4 and CSS, very few HTML elements supported both border and padding, so the definition of the width and height of an element was not very contentious. However, it varied depending on the element. The HTML width attribute of a table defined the width of the table including its border. On the other hand, the HTML width attribute of an image defined the width of the image itself (inside any border). The only element to support padding in those early days was the table cell. Width for the cell was defined as "the suggested width for a cell content in pixels excluding the cell padding."

CSS introduced margin, border and padding for many more elements. It adopted a definition width in relation to content, border, margin and padding similar to that for a table cell. This has since become known as the W3C box model.

RegDwight
+1  A: 

The reason why it's like that is that technically the width of elements is supposed to apply to the content, not the container.

According to the CSS1 specification, released by the World Wide Web Consortium (W3C) in 1996 and revised in 1999, when a width or height is explicitly specified for any block-level element, it should determine only the width or height of the visible element, with the padding, borders, and margins applied afterward.

More info about this behavior*

* Disclaimer: Yes, this is my own blog and I think I did a thorough job of explaining the box model so I'm putting it as reference.

GoodEnough
A: 

If the box model did not work this way, how would you deal with padding around an image? Would you prefer that the size of an img element with padding not match the image's pixel dimensions? Or that the padding covers the image?

It's better that the default behaviour is that the width of the container is not affected by padding or margin values.

ghoppe
A: 

""If the box model did not work this way, how would you deal with padding around an image? Would you prefer that the size of an img element with padding not match the image's pixel dimensions? Or that the padding covers the image?""

First of all, any good web developer would know better than to put an image into a container where it doesn't fit. That is developing 101. If the padding doesn't allow for the image, the image or the padding should be changed. Pure and simple. So the argument mentioned above is faulty.

Padding is an internal setting, internal to the boundries of the container. So when something is inside that container, and you increase the container's padding, the item(s) inside that container should coded so the can be reduced in size.

The word "padding" itself says it all. Can you imagine if UPS added padding to thier boxes to protect the contents inside, only to find that the box increases in size! Rediculous, right? Of course it is! Padding is meant to add space around the inside of a container WITHOUT the container breaking and expanding in height or width.

It's browsers like mozilla, gecko, and opera that have broken box models, not IE. The box model that the "consordium" implements is faulty at best and reaks havoc on web develpers.

If the "consordium" implemented the same box model as IE, than we developers would have a much easier time with the columns of our webpages. I think you have to agree with me on that point. Plain and simple.

I am so tired of people saying that IE is inferior. I can give tons of examples where IE holds strong while the cheaper browsers like firefox break under the pressure.

My two cents. Hate me if you want, but what I speak is common sense and nothing else.

John
I think all web developers hate you for thinking IE is better. Bugs in IE (even IE8) cause all of us endless hours of work. I have yet to find a bug in IE9, but since it's not supported on XP, I might as well not even dream...
tster
-1 There are so many so many things that are wrong about this answer I don't know where to even begin
Yi Jiang