This post will cover how I worked around some z-index background issues I was having. In a recent project I was working on I wanted to use a “div” underneath a bunch of list items/anchor tags. I wanted to see the “div” above the rest of the items but below List items. While it’s pretty trivial doing it if NON of the items in the DOM tree have a background applied to them, I needed to use a background color.
*NOTE* – Before I go any further you might be asking why I didn’t just us a background color using CSS for each item. I couldn’t use the easy way because I eventually I wanted to slide the “highlight” from the left to where ever the person was holding their mouse.
Here’s an example of what I wanted to make:
Unfortunately I kept getting something like this:
Let’s look at the code to see how this issue can be resolved quite simply.
XHTML
CSS (That didn’t work)
CSS (That did work. I’ve highlighted the line that fixed the issue)
So basically I always thought the z-index on HTML elements was “0”/Or not set. This doesn’t seem to be the case. It looks like they are always set to 1. Another really important thing to include is the “position: relative”. If you were to excluded that from the code it wouldn’t work either.
Leave a Reply