views:

21

answers:

2

Here is my issue--I need to somehow access the onclick of an item that is covered by another element of higher z-index. I know this is going against the point of z-index, but any ideas?

In the below example, only the small top-sliver of the red box is clickable. I have a webpage design where tabs that need to be clickable are overlaid by an artsy bar... I'd love if there were a way (maybe some javascript trick?) to use onclick for these obscured, lower z-index elements without changing any positioning, though my gut feeling isn't good.

<html>
<head>
    <style type="text/css">

        #bg {
            position:absolute;
            width:500px;
            height:500px;
            background:pink;
        }
        #under {
            cursor:pointer;
            margin-top:-10px;
            background:red;
            width:50px;
            height:50px;
            z-index:0;
        }
        #over {
            position:absolute;
            width:900px;
            height:50px;
            margin-top:10px;
            z-index:100;
        }
    </style>
</head>
<body>
    <div id="bg">
        <div id="under" onclick="alert('hi');">aaa</div>
    </div>
    <div id="over"></div>
</body>

+1  A: 

I would do it with a transparent PNG inside a DIV above the artsy bar with the same dimensions as your clickable lowest z-index DIV.

Be aware of Internet Explorer Issues.

I used the technique many times.

Stephan Kristyn
Thanks for giving me the answer. Let us know if it works.
Stephan Kristyn
A: 

The usual replacement method is to place the "over" elements (positioned absolutely) inside the "under" elements (positioned either relatively or absolutely) and make the content the same size (Gilder/Levin Image Replacement).

Stan Rogers
But an element can't have a higher z-index than its parent..
Rujiel
Oh, yes it can. Everything visible on a web page is a descendant of the body element, and you can certainly set elements on the page to a z-index higher than the body. I have personally built many tens of websites over the past decade or so that used exactly this method, since it was the only images-off/css-on accessible method for image replacement known at the time it was introduced.
Stan Rogers