IE, z-index, and making a DIV a link
Recently I had the need to turn a DIV into a clickable link. This sounded like a fairly easy task until I got into it and ran into a few issues with Internet Explorer (IE). One of the challenges also was that I wanted to avoid using Javascript… I felt this needed to function without the need for Javascript. I’ve included a jQuery solution from Jeff Croft at the end of this post for reference.
Here’s the code I came up with to make things function:
<div class="project">
<a href="/link/to/project" class="block-link"> </a>
<a href="/link/to/project"><img src="/img/project-thumb.jpg" alt="Thumb for project X" /></a>
<h3><a href="/link/to/project">Project name</a></h3>
<p>This project let use flex our design and code muscles to create a truly cool site.</p>
</div>
.project { position: relative; }
.project .block-link {
display: block;
text-indent: -9999px;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
This gave me a nice DIV to hold my content while making the whole thing clickable. Things worked great in Safari and Firefox, however when I went to check in IE6 and IE7 life wasn’t so good. What ended up happening in IE was that my clickable overlay was underneath the image and text. Doing some searching on Google yielded various fixes to the problematic way IE handles z-index. No amount of z-index value changing or other variety of documented fixes were working in my situation.
So I started doing some trial and error research to see what might trigger a resolution. Because my clickable overlay was empty I thought content may help resolve the issue. I tested this theory and found that having a background color made things work as expected and as they behaved in the other browsers. Finally I was getting somewhere, so I ended up adding a transparent PNG background to the clickable overlay and all was good. So it seems IE doesn’t like DIVs lacking content to be above other elements, so faking content with a transparent background resolved the issue I encountered.
With this I can now nicely create an overlay for repeatable div elements that can occur on a listing page. I hope this helps some of you out there!
// Allow elements with the class “clickable“ to be, well, clickable.
// This basically emulates HTML 5 functionality that isn’t very well-supported yet.
$(”.clickable“).click(function(event) {
event.preventDefault(); window.location = $(this).attr(“href”);
});

Comments
http://stackoverflow.com/questions/180211/jquery-div-click-with-anchors
I know I have to assume that you want all markup to validate, but by way of having all the extra markup you defeat the purpose of forcing yourself to write valid code over making things easier on yourself and writing more simple code…
why not display: block; the a element with the same structure that you use for the containing div.project? use the a as the container itself. so you’ll throw a couple block-level elements inside it—big deal, you’re promoting it to block-level via css, so it’ll still work. you’re cutting down on extra divs, extra css, and not having a big workaround just to validate. If you comment your markup for the validation snobs out there (and I can be one of them at times) so that they’re aware that you’re trading invalid code for less unnecessary markup, then you’ll likely earn praise rather than flames. and if not, screw ‘em. too many people make things too difficult (again, myself included at times) in order to validate. be happy that you found a better solution instead and go with it. it’s not the only option, but it is an option.
Jeremy,
Part of the reason I chose to stick with the DIV as opposed to an A tag was the structure of the information. The situation made sense to have a DIV. I wanted a DIV to be a link and not a link to be a DIV.
So that’s partly why I went the route I did. Your suggestion would also be fine, however in this case the DIV made more sense to me.
Thanks for the feedback!
and there again lies the beauty of the web. so many ways to accomplish the same task, and legit reasons for passionate arguments for all of them. I love when designers offer real-world implementations as the basis for a blog post and the community responds with insight and alternatives. you got yourselves another feed subscriber. keep it up!
Nice info thanks