Fixed position and z-index

Yes, those fixed-positioned elements are annoying; please bear with me.

The two fixed-position divs on this page are marked up as:

<div class="fixed" id="green"></div>
<div class="fixed" id="orange"></div>

and styled thus:

.fixed { width: 50%; margin: 0 auto; height: 50px;
    position: fixed; }

#green  { top: 50px; background-color: #CEE14B; }
#orange { top: 110px; background-color: #FCAF3E; z-index: 1; }

Note that the lower, orange element has a z-index specified, whilst the first element doesn’t. The ul just below this text has a relative position. When the page is scrolled, and the orange div overlaps that list, all looks fine. When the green div overlaps it, however, the list items are shown in front of it.

Is this a bug, or expected behaviour? I’m really not sure. Comments, please!


Comments

Sat 28 Nov 2009 09:57

Danny

Danny said:

expected behavior…

Since the green DIV has no z-index priority… it is on the same plane as every other non z-indexed element on the page. The indexing now falls to where it’s placed in the code. Since it’s before the UL list… it has a lower indexing on that plane… If you place the green DIV after the UL list… you'll see that it now goes over the UL list… thus having a higher indexing on that plane…

Fri 22 Jan 2010 12:30

raf

raf said:

Sounds plausible and works as you described, Danny. But how come the paragraph following the list, "Is this a bug…", does not behave the same way? Why does this behaviour only apply to the list?

Sun 24 Jan 2010 11:27

Five Minute Argument

Five Minute Argument said:

@raf: That paragraph is not positioned, so it’s in the normal flow. With no z-index (and, therefore, the deafult value 'auto'), the positioned green div will be rendered 'in front of' any normal-flow block.

z-index gets pretty complicated, especially when nesting blocks or applying a negative value to the property. I think a nice visual example covering the possible cases is called for.

Leave a comment