tags:

views:

53

answers:

3

I understand that the height of a box in CSS is the height of the contents, excluding the margin and padding, but why with this sample, if you uncomment the border: line in the containing div, does the background color of the div extend above the first paragraph while if you have no border it doesn't?

<html>
<head>
    <LINK REL=StyleSheet HREF="style.css" TYPE="text/css" MEDIA=screen>
    <title></title>
    <style type="text/css">
        #container {
            background-color: green;
            /* border: black solid 1px; */
        }

        p { background-color: red;
            margin-top:50px;
            margin-bottom: 0px;
            border: black dotted 3px;
          }
    </style>
</head>
<body>
<div id="container">
    <p>first paragraph</p>
    <p>second paragraph</p>
</div>  
</body>
</html>
+2  A: 

I understand that the height of a box in CSS is the height of the contents, excluding the margin and padding

Wrong: it includes padding and border (except in Microsoft Internet Explorer due to a bug and now for compatibility reasons (if using quirks mode rendering)). Read up on the CSS box model:

The content edge surrounds the rectangle given by the width and height of the box

where the content edge is the edge running around the outside of the border.

Konrad Rudolph
@Konrad Rudolph, he did no set width and height to #container element thus sizes are useless.
eugeneK
But why does the presence or absence of the border alone change the amount of background green that's visible above the first paragraph. I want to understand the why here - I'm not trying to achieve any particular result. Thanks!
@aizuchi, you've got plenty of errors in CSS code. It's hard to tell why unless you fix an error. Box model ( the way browser calculate size of certain element ) is different for ie.6(and below) and other browsers. This means if you give specific width and height and then apply margin,padding an border each browser will render differently.Since you haven't provided us information about browser you use, we can't you help that much
eugeneK
This is in Safari. Try it yourself - it's a very interesting result. What are the errors in this CSS code that you see?
The behavior is the same in Firefox and Safari - I think this is standard behavior, I'm just trying to understand why. Thanks!
@aizuchi, @eugene: I was merely commenting on the wrong premise. The fact that setting a border changes the margin behaviour is related but distinct. Unfortunately, I don’t have the time to answer that, too.
Konrad Rudolph
A: 

Margin is on the outside of a border and padding is on the inside of a border, so your top margin would cause the margin to exist above the border. If you want the padding between your paragraph and border use padding not margin. The size of the div will be determined by the margin, padding, and border. They will all contribute to the size of the div.

Brendan Enrick
+1  A: 

@aizuchi,

First of your CSS has an error. Check for "pic" right to "margin-bottom".

Second of all add "overflow:hidden;" to #container element, once you haven't set siez of parent element you must have this tag to tell parent which size to use. It will make #container to use height of child element at it's own (#container) which is probably the problem in your CSS besides "pic".

Third of all, Google box model bug in IE6 to understand difference between our "beloved" ie6 and other browsers.

Fourth of all

it is better to use

<LINK rel="StyleSheet" href="style.css" type="text/css" media="screen" />

instead of

<LINK REL=StyleSheet HREF="style.css" TYPE="text/css" MEDIA=screen>
eugeneK
Error fixed - thanks! But I still don't understand why the presence or absence of the border changes the amount of background green showing in the container div....
Did you set overflow:hidden; on #container ?
eugeneK