Avtex Blog: News and tips on technology solutions

How to make an entire div clickable with CSS

Sometimes you want to make an entire div (or other element) into a clickable link. Here’s an example.

Here’s a cross-browser way to do it using pure CSS:

HTML:

<div class="clickable">
    <a href="URL_OF_LINK_TARGET"> </a>
    Rest of div content goes here
</div>

CSS:

div.clickable { /* Containing div must have a position value */
    position:relative;
}
div.clickable a {
    position:absolute;
    width:100%;
    height:100%;
    top:0;
    left:0;
    text-decoration:none; /* Makes sure the link   doesn't get underlined */
    z-index:10; /* raises anchor tag above everything else in div */
    background-color:white; /*workaround to make clickable in IE */
    opacity: 0; /*workaround to make clickable in IE */
    filter: alpha(opacity=1); /*workaround to make clickable in IE */
}

First, give the containing div position. That way, when we apply “position:absolute;” to the link (see next paragraph) it will position itself relative to the containing div.

Next, make the link absolutely positioned and the full size and depth of the containing div. The link’s z-index makes sure it’s above everything else in the div, so no matter where you click, you’re clicking the link.

IE, naturally, has quirks. In this case, IE requires a background color for such a link to be clickable, so we give it a background color (white), set the opacity to 0, then give it an IE-only opacity of 1% using IE’s proprietary filter property.

Finally, put whatever content you want in the div. If you’re going to be layering the content using z-index, just make sure not to give any element a higher z-index than the link.

Written by

I've been an information designer of one sort or another -- print, web and combinations -- for 20 years. As a member of the Creative Services team at Avtex, I design and build interfaces for websites and applications. I go from initial concept through working wireframes to final storyboards, then help build the real thing with SharePoint, Wordpress, CSS, HTML, jQuery and PHP.

9 Comments to “How to make an entire div clickable with CSS”

  1. stan says:

    thanks! just what i needed

  2. BitBug says:

    Very nice post. And great, clean CSS. I poked around the web for a while looking for a better solution that what I was using and came across your site. Your solution is the most comprehensive, neat and tidy pure CSS solution I have found.

    Nice work!

  3. Josef says:

    Hi! Thanks for the code. However, I would like to have an image in the “clickable” div but when I do so it is hiden behind the white background color. How could I place an image behind the clickable div in such a manner as it is visible?

  4. Josef says:

    Got it mate! I just used a transparant background image instead of the white background color. Works like a charm in IE as well. No need to do the workaround.

  5. Djela says:

    Amazingly useful! Most solutions were javascript / jquery. Yours is the only css solution for clickable div I came across. Many thanks!

  6. Yasel says:

    Very good approach, congratulations for your excellent job. It solved my day!

  7. kesadisan says:

    Thank you so much for this workaround, XHTML is really annoying when it comes to div wrap anchor

  8. JT says:

    Thanks! Works like a charm.

  9. Juan says:

    worked perfectly… simply and elegant

Leave a Reply

Message