Forum Moderators: not2easy

Message Too Old, No Replies

The Box Model and Margins

width + padding + border = box - but what about margins?

         

MatthewHSE

11:54 am on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So I understand the standards regarding width, padding, and border on the box model. But where does margin come in? If I understand correctly, it's just one more measurement you would add to the width, padding and border in order to tell the entire width occupied by the box and its "extras," right?

In that case, wouldn't one way around the IE 5.x box model problems be to apply a margin and a width, instead of padding and a width?

Longhaired Genius

12:09 pm on Oct 21, 2004 (gmt 0)

10+ Year Member



Yes. Mayn people do something like this. My approach is not to give my boxes any padding but to give the elements within, paragraphs, headings, etc, margins such that they line up satisfactorily. You only have to take the time to do this once or twice and they you can copy the css values from site to site.

photon

12:49 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you just use margins, though, you could run into problems with collapsing margins throwing things off.

Some people use two nested divs, one with margin, the other with padding.

createErrorMsg

1:20 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Padding is the space between the content area and the border of the box. Margin is the space between the border of the box and any adjacent boxes.

See this W3 diagram [w3.org] for a visual.

All three (margin, padding, border) are added to the box width in compliant browsers, and subtracted from the box width in IE.

rich_b

1:55 pm on Oct 21, 2004 (gmt 0)

10+ Year Member



>> All three (margin, padding, border) are added to the box width in compliant browsers, and subtracted from the box width in IE.

Is that correct? I'm no expert so I may be misunderstanding this or my source may be incorrect but at [positioniseverything.net ] they say that IE5.x includes the content, padding and borders in its width calculations but not the margins.

Also according to them, IE6 in 'Standards mode' (with a 'Standards' Doc Type declaration like Transitional - loose.dtd) will adhere to the standard box model as used by Opera and FF. IE5.x can easily be fixed using the Tan hack. So if all this is true surely we don't have a major box model problem if we can force IE6 into 'standards' mode? We code our CSS to the W3C standard and use the Tan hack for IE5.x (ignoring NN4/IE4) - job done?

I'm bound to be missing something major but I'd appreciate knowing what!

photon

5:08 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're not missing anything rich_b. That's actually a pretty good summation. Margin is not included in IE's non-standard box model.

The only thing I would change is to add that for my money there are better "hacks" (perhaps we can call them "techniques" :) ) to use.

By the way, here's [redmelon.net] a rotating 3D diagram of the box model.

[edited by: photon at 5:17 pm (utc) on Oct. 21, 2004]

createErrorMsg

5:09 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's weird. I've read that PIE page several times and never noticed that it doesn't factor in margin to box width.

However, margin clearly does affect the calculated width of a box. try this...

<html>
<head>
<style type="text/css">
#wrapper{
float: left;
width: 400px;
background: white;
}
#box1{
float: left;
width: 200px;
height:100px;
background: blue;
}
#box2{
float: left;
width: 200px;
height:100px;
background: yellow;
/*margin: 0px 10px;*/
}
</style>
</head>
<body>
<div id="wrapper">
<div id="box1">Lorem ipsum dolor sit amet.</div>
<div id="box2">Lorem ipsum dolor sit amet.</div>
</div>
</body>
</html>

Load it in IE or FF (since there's no doctype, it'll throw them into quirks mode and display the same as it would in IE5.x). You'll see that the yellow and blue boxes float side by side.

Now uncomment the margin property in #box2. The yellow box now moves below the blue box. Why? Because there isn't enough room in #wrapper for two 200px wide boxes PLUS 20px of margin.

I suspect they say margins are left out because a margin exists outside of the border, and common sense tells us that a box ends at it;s borders. But clearly margin has an effect on the IE box model problem and must be factored in when calculating box widths for a layout.

photon

5:21 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Think of the boxes as PCs. If you're storing them, you can stack them up against each other, and figure out how much room you need just from the dimensions of the case.

However, if you've got a server farm, you're going to need a "margin" between them for air circulation/heat dispersion. Even though the dimensions of the cases haven't changed, you're going to need more room for them.

Ain't analogies grand? ;)

createErrorMsg

6:07 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All I know is that you have to shrink the IE width value by an amount equal to the left and right margins on a box or IE5.x and 6/Quirks break the layout.

createErrorMsg

7:37 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Load it in IE or FF (since there's no doctype, it'll throw them into quirks mode and display the same as it would in IE5.x).

Interesting.

If I were right, and margins were stuffed into the Quirks box model, the example I give in post 7 would only exhibit the stacking behavior in compliant browsers. But with no DOCTYPE, that example ought to be throwing IE6 into Quirks mode, which would pack the margin inside of the box and NOT cause the stacking.

The fact that IE6/Quirks stacks the boxes seems to prove that margins are added to the outside of a box no matter what box model is being used. Which was rich_b's point all along (I think).

So I was wrong: while it IS necessary to allow for margins when widthing your boxes, it appears this allowance must be made for all browsers, even the ones with the broken box model.

rich_b

9:13 am on Oct 22, 2004 (gmt 0)

10+ Year Member



Photon - Which techniques would you recommend over the Tan hack?

createErrorMsg - that's an interesting example you posted there. Unfortunately my brain just exploded after trying reading all the posts again. I shall try again later to get my head around all of this!

jetboy_70

10:47 am on Oct 22, 2004 (gmt 0)

10+ Year Member



You will *never encounter any box model problems* if you refrain from:

. Setting any horizontal (left or right) padding or border on any element on which you have declared a width setting.

. Setting any vertical (top of bottom) padding or border on any element on which you have declared a height setting.

These points are not particularly restrictive.

If you want a border on an element where you have declared dimensions, put it inside another element that just has border declared but no dimensions. It will 'shrink wrap' the inner element giving the desired effect.

If you want padding on element where you have declared dimensions, put another element inside it that just has padding declared but no dimensions (as mentioned by photon). It will be pushed out by any elements it contains, giving the desired effect.

Minus point:

. Extra HTML elements which exist solely for layout instead of content.

Plus points:

. No hacks
. No need to maintain two sets of dimensions for different browser models
. No more worrying about the box model - it ceases to be an issue. :)

photon

1:56 pm on Oct 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's an older thread that talks about avoiding the hack: [webmasterworld.com...]

Here's a newer one that comes very close to a universal cure: [webmasterworld.com...]

Personally, I just write code that validates, check it in FireFox, and tweak for IE. My layouts are generally fairly simple (I like to think of them as "clean" :) ), so I've rarely had need of a hack. The most I've ever done is use the technique jetboy_70 has described.