tags:

views:

28

answers:

1

The box model ought to be simple enough, but I just don't grok it.

Here's a meaningless form

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
<title>Box model test</title>
<link type="text/css" rel="stylesheet" href="test.css"> 
</head>
<body style="position:relative; top=0px; left=0px">
<form action="for_submitted.php" method="post">

<fieldset style="top:0px; left: 0px; width: 128px; height: 64px;">
  <legend> </legend>
  <div class="label" style="top:24px;  left: 16px; width: 81px; height: 14px;">Box 1</div>
</fieldset>

<fieldset style="top:0px; left: 128px; width: 128px; height: 64px;">
  <legend> </legend>
  <div class="label" style="top:24px; left: 64px; width: 22px; height: 14px;">Box 2</div>
</fieldset>

<div class="submit_button" style="top:64px; left:64px;"><input type="submit" name="submitButton" 

value="Submit"></div>

</form>
</body>
</html>

and its CSS

body
{
  background-color: white;
  width: 100%;
  position: relative;
}    

.panel, .label, fieldset
{
  position: absolute;
  font: 8px Arial; 
}

.submit_button
{
  position: absolute;
}

and I expect it to show

.---------..---------.
| box 1   | box 2    |
.---------..---------.

but in MSIE8 (which I am compelled to use) and in FireFox, it shows

.--------..--------.
| box 1  || box 2  |
.--------..--------.

why do they overlap?

+1  A: 

There are several problems here. First of all, you need a reset CSS to remove the margin and padding added by the browser's User Agent stylesheet. Something like Eric Meyer's will do.

Secondly, after adding in the reset CSS there still will be some overlapping. This is caused by the borders, the widths of which are not part of width. outline is not affected by this, so I'm using that in the demo below to show that the elements are not actually overlapping, but I won't recommend using that in place of border. Instead, you should take into account the width of the border when doing your calculations.

See: http://jsbin.com/ofusa3/edit

Yi Jiang
+1 This is a great help! And I noticed that you very very helpful on my previous questions too. Thanks. Ok, I liek the idea of a reset file. Now, if you look at the diagram of http://www.w3.org/TR/CSS2/box.html then I presume that 'outline' is teh outermost dotted line? And I was mistakenly thinking that that was the border. So, if I understand you, you are saying that I should add X to top/left and subtract X from width/height to achieve the same visually as you did with outline? And X = border width? Nvm, I will play around with it. Thank you very much.
LeonixSolutions
Shoudl I add X to top/left, subtract 2X from width/height and add a margin if X?
LeonixSolutions
@Leonix No, that's not what I mean. The `outline` property is usually used for "debugging" CSS positioning problems, since it doesn't affect the total width of the element. Maybe instead of reading the actual specs, which are usually too technical, something like: http://dev.opera.com/articles/view/30-the-css-layout-model-boxes-border/ will help you understand this better
Yi Jiang
@Leonix Also, `outline` is applied in the same manner as `border`, except that it is rendered in such a way that the final width of the element is not affected. Final width here being `width` + `border-width` + `margin`
Yi Jiang
LeonixSolutions
@Leonix You should take `border` into account here by subtracting `border-width` * 2 (for either side of the box) from the width entered by the user. That should be the correct solution to this problem. `outline` does not render consistently with some elements and IE 6 doesn't support it.
Yi Jiang
+1 and the answer - thanks
LeonixSolutions