Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox body margin bug?

I have this simple example:

<header>
    <ul>
        <li>Item</li>
        <li>Item</li>
        <li>Item</li>
        <li>Item</li>
        <li class="clear"></li>
    </ul>
</header>
<section>section</section>

And this piece of css:

BODY, HTML{
    margin: 0;
}
header{
    margin-bottom: 100px; /* section goes down */
}
UL{
    list-style-type: none;
}
UL LI{
    float: left;
    background: green;
}
.clear{
    clear: both;
    float: none;
}
section{
    background: red;
}

So I expecting to "header" goes straight to top-left corner, then 100px margin, then "section". In all major browsers that works as expected, but in Firefox (version 16) "header" get extra margin-top for some reason.

Is this a bug?

Here an jsfiddle example: http://jsfiddle.net/AvZek/2/

BTW If I used clearfix instead of "clear" class than it's working just fine.

like image 395
Andrew Terentyev Avatar asked Oct 25 '12 14:10

Andrew Terentyev


People also ask

How do I get rid of default body margins?

This margin is automatically created by some browsers to allow for space between the edges of the viewport and the website content. You can remove this margin by setting the top and left margin to zero.

What margin 25px 50px will do?

margin: 25px 50px;top and bottom margins are 25px. right and left margins are 50px.

How do I set body margins in HTML?

You can control the margins on the body using CSS. The CSS property for just the margin on the top of an element is margin-top (not topmargin ). However, it is usually a better idea to use padding on the <body> , rather than margin.

Do browsers have default margin?

In most major browsers, the default margin is 8px on all sides. It is defined in pixels by the user-agent-stylesheet your browser provides.


2 Answers

This is, without a doubt, a bug. The margin is definitely not supposed to be there.

  • According to Firebug, the only non-zero margin that is computed that I can see is the same margin-bottom: 100px on your header element as in your CSS. Everything else is zero.

  • Even Firebug's DOM inspector has trouble identifying it; it never highlights that region, with the obvious exception of when you're inspecting html itself (which it highlights as part of its content area).

I found tons of bug reports which were closed as duplicates of this one, with numerous more test cases. Plus, it looks like it has been around since at least Firefox 2.

like image 68
BoltClock Avatar answered Sep 21 '22 16:09

BoltClock


Instead of polluting your code with non-semantic and unnecessary empty <li>-s just add overflow: hidden to your <ul>

HTML

<header>
    <ul>
        <li>Item</li>
        <li>Item</li>
        <li>Item</li>
        <li>Item</li>
    </ul>
</header>
<section>section</section>

CSS

ul {
    list-style-type: none;
    overflow: hidden;
}

DEMO

like image 27
Zoltan Toth Avatar answered Sep 21 '22 16:09

Zoltan Toth