Recent Projects

Make a Div Into a Link

It’s easy to make a div into a link using a bit of javascript. You can use this technique to make any div “clickable”. For example, you might want your “header” div to link to your home page. Here’s how:

<div onclick="location.href='http://www.example.com';" style="cursor:pointer;"></div>

If you’re using a standard WordPress theme, you can make your header div clickable by doing this:

<div id="header" onclick="location.href='<?php bloginfo('url');?>';" style="cursor:pointer;"></div>

That’s it!

86 Responses to “Make a Div Into a Link”

  1. Nice idea! (I just wish it didn’t rely on Javascript…)

  2. jake says:

    This makes life so much easier. Though.. would there be a way to alter the overstate of a link inside the div. I have a table inside and I can’t change that at this time …. the table has a link within to the same page.

    Any thoughts?

  3. Gloria says:

    Why does the first method (the one with Javascript) not work in IE? Anyone know of any IE browser hacks I could use? Thanks!

  4. Darthur says:

    So good! Thanks!

  5. Nick says:

    Excellent technique!

    We have used it in a nice new design on our site.

    Thanks!

  6. Mike Toews says:

    Great, I’ve been wanting to do this for a while.

    Thanks

  7. You helped me tremendously. Thanks!

  8. i got my links problem solved. thank you.

  9. anonymous says:

    This does not *really* make the div into a link. It just makes it clickable. If you right click on the div you do not get the menu options to copy the link target or open the link in a new window/tab that you have for *real* links.

  10. anonymous says:

    So my DIV responds to clicks now, but how do all the buttons in my DIV from bubbling up their click event to the DIV’s handler? I don’t want to add “e.stopPropagation()” to all my contained buttons/links…

  11. Sky says:

    Thanks for the help — simple and effective.

  12. [...] Few days ago, I was looking for the best way to make a DIV into a link when I came across this website: Almost Effortless!. The author showed how to make a div a link with a little bit of Javascript (see article). It is actually pretty simple: [...]

  13. Bingeboy says:

    thanks for this post! Nice clean and easy.

  14. GaB says:

    Great tips thanks!

    @Jake: I know it’s an old comment but did you try ‘div table a:hover’ in your css ?

  15. Callie says:

    Wow, that helped alot. Thank you!

  16. Devin S. says:

    I think it would be better to put an rel=”nofollow” inside of the link and using CSS to set ‘display: block;’, ‘width: 100%;’, and ‘height: 100%;’ on the link. It would be better if you knew the width and height of the containing to set in the CSS instead of using 100%.

    Just my opinion. But, 6% of people who visited w3schools.com didn’t use JavaScript.

  17. seutje says:

    thank u Devin S. for making this page worth loading up ;)

  18. Urban says:

    Good good tnx :D

  19. Kyle says:

    This is brilliant, thanks very much!

  20. Ash says:

    Any one know how to make it highlight when hovering with a mouse?

  21. LeWebDev says:

    this is definitely not accessible, intrusive et generally …not very clean. :/

    You shouldn’t use it.

  22. Djela says:

    simple, elegant and so useful.
    thanks for sharing.

  23. matt says:

    awesome, thanks.

  24. great, I didn’t know I could do that. Will use it right away

  25. IAtect says:

    Screw people who don’t use JavaScript! This works perfectly!

  26. asdasdasdasd says:

    exactly what i needed.. thanks!

  27. Skinny says:

    Ok this works but I can not figure out where to place the target=”blank”

  28. Skinny says:

    I do not believe that the block style will work in my situation. I have a drop down menu over the “div” If the drop down passes over a block container I lose mouse function in the drop down menu.

    Still no solution on making the target=”blank” when the div is a link.

  29. Hankieh says:

    Thnx it works :)

    I first made in my css file the link with div#name a {}
    but than your class=’name’ becomes id=’name’ and you can’t use style=’left: px; top: px’ anymore

    So this is perfect :)

  30. Qvr says:

    target=”blank” can be done with:

    onClick=”window.open(‘http://www.cnn.com/’,'cnnWindow’)”

    The above opens in a new tab in Firefox, the following opens in a new window:

    onClick=”window.open(‘http://www.cnn.com/’,'cnnWindow’,'width=400,height=200′)”

  31. Skinny says:

    Still no solution on making the target=”blank” when the div is a link.

  32. SEO Guy says:

    oooohhh man its times like you think whish more people used javascript : (

  33. i still dont get it, how can you link a div? : (

  34. Marinus says:

    So many people, so many wishes…

    Worked for me in the first try. Flawless, Effortless.

    Great! :D

  35. aris says:

    Oops! looks like the comments are html sensitive. Again:

    Accessibility, diasabled javascript, context menu behaviour and lack of status bar feedback be partially resolved this way:

    Inside the <div> make sure you also have an <a> with the same link as the javascript. So, enclose the main image or text line in that anchor. At least over that anchor, all standard behaviours work and this method then takes care to fill all visual gaps around that main link.

  36. j. kouri says:

    Three years later, and this article is still helping people. Thanks a lot, this was just what I needed.

  37. Cam Peterson says:

    Love it. An effortless solution to my prob!!!

  38. Ricky says:

    Effortlessssss ……………..very good yar

  39. icooci says:

    wow thank you for this…exactly what i was looking for, first in google search!!!

  40. James says:

    Ah damnit.. div script disappeared.. try this..

    onclick=”window.open(‘http://www.mysite.com’,'_blank’);” style=”cursor:pointer;

  41. seda says:

    thanks it worked!

  42. sweet, I can even do it. Thanks!

  43. Phil Knoll says:

    How’s about:


    Will it ever stop working?

    XHTML 2 has *all* elements linkable (the anchor tag is no more)… ’tis the way of the future.

  44. adam says:

    Cheers worked a treat

  45. Yuri says:

    I use and it works with IE, Opera and Firefox.

  46. smudgedlens says:

    I’m pretty sure you can implement this same thing just with CSS, right? I found something awhile ago that showed how to do it, but I can’t remember now…

  47. E. says:

    Nice. Thanks! Worked on first try.

  48. Niklas says:

    Not a good idea, it won’t work without javascript.
    Use CSS instead:
    HTML:

    CSS:
    div#button a {
    display:block
    }

    Never use javascript for layout unless absolutely necessary (and make sure all javascript functionality has a fallback for users with javascript disabled)

  49. K says:

    Excellent. You just saved my life. Works in all browsers combined with the dropdown divs I’m using. Thank you!

  50. Matt says:

    Thanks; great reminder!

  51. This was simply outstanding. Works beautifully in firefox and ie.

    Thanks buddy :-)

  52. Johnny says:

    Great, G R E A T, thank you

  53. ThangNguyen says:

    search on google on redirect to this page, it’s work… three years later :D

  54. Faruque says:

    but there is a problem if i wanna open the link as a ‘new tab’

  55. Byron says:

    Thanks, I really needed that.

  56. taste twist says:

    I dont understand can you explain -using wordpress if you are creating a new div or copying the code – inside the picture header div – thanks

  57. Iva says:

    Worked a treat. Thanks!

  58. Ronan says:

    You beauty! thanks for this you are a life saver.

    R

  59. derry says:

    Perfect! Thanks for posting this.

  60. Fco says:

    Thanks, i have been searching for this for a while.

  61. nadim says:

    Thanks, this help me lot.

  62. Awesome little technique, thanks for sharing it! – Dave

  63. kasie says:

    worked perfectly and still kept my code valid. thanks!

  64. gweedo says:

    Thanks… a heck of a lot easier than tracking down an image unnecessarily!

  65. horse says:

    can’t believe it. great little ace in the hole. how’s the usability in 2009?

  66. nadeem says:

    Thanks for your great help!

  67. Daveo says:

    This is great and a nice easy clean tip thanks heaps
    BUT…
    I can’t seem to get it to work in IE7 when it is a nested div in a containing div. Works fine in FF.
    Any ideas?

  68. suraj says:

    Thanks for your helpp. It helped me

  69. Hashmat Abbas says:

    Thanks a lot….

    It really helps

  70. June says:

    how do I get the link to open in a new window.

  71. jfr says:

    Thanks great help just what i needed!

  72. JoeShmoe says:

    THANK YOU!!!!!!!!

    Worked 100%, simple and easy. Thanks again

  73. Mark Martin says:

    Worked like a charm! Many Thanks.

  74. Alfonso says:

    lifesaver, thanks!

  75. SuSu says:

    Thanks !

  76. Alan says:

    worked for me! thanks

  77. Pavelas says:

    working good:)

  78. martin says:

    Its great!
    Thanks!

  79. Drew 7 says:

    Almost 4 years later and this snippet is still helping people.

    Thanks!

  80. Susan says:

    Simple and elegant what more could you ask for

  81. Jenny says:

    works great! but wont validate for me :(

  82. If you want to make a div transparent, this is a very nice solution.

    But if you want another one, a bit more to write in codeway, but imho better is like i have done on my site.

    1: Create a file, 1×1 px. Call it transparent.gif

    2: Add this to your div:

    That way you make a transparent picture inside the div that you can make as big/small you want. The tranpsarent picture is then linked.. Easy and xhtml fine.

    For wordpress you can use this (makes your header pic linkable to the startpage) (Put your transparent.gif inside your images folder in your templade folder)

    <a href="”><img src="/images/transparent.gif” width=”988″ height=”300″ alt=”Header” />

    Obviously, change the width and height to whatever suits you.

Leave a Reply