3

I have a simple horizontal menu with two levels. This menu stretches to 100% width of the wrapper. Here is the fiddle: http://jsfiddle.net/gpsgv/

If you run this fiddle in any browser except Firefox, it displays the following, as expected:

enter image description here

If you run this fiddle in Firefox, it displays the following:

enter image description here

Looking at the code, the second level lists are absolutely positioned inside the first level items (which have display: relative style). So, setting the left: 10px style to second level list should position it 10px from the left side of its relatively positioned ancestor. Similarly for top: 30px. But in Firefox, instead, it positions it on the left side and at the top of I don't know what, maybe the body?

My question is, is there any solution to make it display correctly in Firefox, without changing the HTML?

P.S. I use display: table-cell because the menu must be spread evenly along 100% container width.

camcam
  • 2,585
  • 8
  • 49
  • 65

3 Answers3

10

Without changing html - anyway.

position:absolute forces display: block, read here: http://www.w3.org/TR/CSS2/visuren.html#dis-pos-flo

Solution: wrap your menu item content to other element:

<li> 
    <div class="menu-item-container">
        <a href="#">Item</a>

        <ul>
            <li>
                <a href="#">First</a>
            </li>
            <li>
                <a href="#">Second</a>
            </li>
        </ul>
    </div>
</li>

And css for wrapper:

.menu-item-container {
    position: relative;
}
maximkou
  • 5,252
  • 1
  • 20
  • 41
  • How do you know that it is not resolvable without changing HTML? According to CSS rules it should work, (and it is working, except for FF) – camcam Jul 23 '13 at 11:12
  • @camcam, position: absolute forces display: block, read http://www.w3.org/TR/CSS2/visuren.html#dis-pos-flo – maximkou Jul 23 '13 at 11:15
  • The only element positioned absolutely is the second level list. So, being displayed as block, it should be positioned absolutely within the first level `
  • `. Instead, it is positioned absolutely within the body element.
  • – camcam Jul 23 '13 at 11:33
  • @camcam, first level "li" displayed as "table-cell", not a "block"! This is obviously stated in your code. It is right that is placed relative to the body, read docs carefully please. – maximkou Jul 23 '13 at 11:39
  • But the problem is not with first level `li`, it's with second level `ul`. The problem is, why FF doesn't see the right ancestor with relative positioning. – camcam Jul 23 '13 at 11:55
  • I think you are right, it's necessary to change the HTML. Here is [a helpful post](http://stackoverflow.com/questions/7629326/position-relative-in-firefox/7629567#7629567). And a [citation](http://www.w3.org/TR/CSS21/visuren.html#propdef-position) from w3.org: _The effect of 'position:relative' on (...) table-cell (...) elements is undefined._ So, that's why my table cell `li` ancestor is simply not positioned relatively, so it's not the valid ancestor for absolutely positioned `ul`. – camcam Jul 23 '13 at 12:47