Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why margin-top of the top div would apply to `<body>`?

Tags:

html

css

Here I posted a demo http://jsfiddle.net/LxYMv/1/.

As you can see <body> gets margin-top:10px from the top div, and therefor <html>'s black background leaks out. Does it mean that I can't give the top div a positive margin-top?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <style>
            html{color:#000;background:#FFF}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,select,p,blockquote,th,td{margin:0;padding:0}table{border-collapse:collapse;border-spacing:0}fieldset,img{border:0}address,button,caption,cite,code,dfn,em,input,optgroup,option,select,strong,textarea,th,var{font:inherit}del,ins{text-decoration:none}li{list-style:none}caption,th{text-align:left}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal}q:before,q:after{content:''}abbr,acronym{border:0;font-variant:normal}sup{vertical-align:baseline}sub{vertical-align:baseline}legend{color:#000}
            html{background:black}
            body{background:white}
        </style>
    </head>
    <body>
        <div style="margin-top:10px;background:red;height:100px">Here the top div begins</div>
        <div style="height:800px">A long long div</div>
    </body>
</html>
like image 670
Rufus Avatar asked Sep 28 '11 06:09

Rufus


People also ask

What is the use of margin-top?

The margin-top CSS property sets the margin area on the top of an element. A positive value places it farther from its neighbors, while a negative value places it closer.

Why margin is not applying to div?

You cannot see the effect of margin-right because the 'width' property of div causes the margin of div to have more than 10px distance from the right wall of the body. Consequently, it causes the margin-right property not to have any visual effect.

Why margin-top is not working in a tag?

This issue is known as Margin Collapse and happens sometimes between top and bottom margins on block elements. That's why the margin doesn't work on the p tag. To make it work here an option is to use padding-top on the p tag. And on the a tag the margin doesn't work because it's an inline element.

Why does HTML body have margin?

Margins are used to create space around elements, outside of any defined borders. This element has a margin of 70px.


2 Answers

This is called margin collapsing.

When an element with a margin is inside an element with no padding or border, the margin will be applied outside the parent element instead of between the child element and the parent edge.

The basic reason for this behaviour is that margin specifies the minimum distinace between elements, not a distance around an element like padding specifies distance around the element content.

like image 162
Guffa Avatar answered Oct 13 '22 21:10

Guffa


You can get around this by adding padding: 0.01px; to the <body>. As it's so small it is effectively rounded to 0px, but the body then starts where it should.

like image 37
Niet the Dark Absol Avatar answered Oct 13 '22 21:10

Niet the Dark Absol