Make a Div Into a Link

Posted by Trevor in Code on May 25, 2006

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!

51 Comments

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

 jake

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?

 Gloria

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

So good! Thanks!

 Nick

Excellent technique!

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

Thanks!

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

Thanks

You helped me tremendously. Thanks!

nice. thanks.

i got my links problem solved. thank you.

 anonymous

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.

 anonymous

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…

 Sky

Thanks for the help — simple and effective.

[...] 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: [...]

thanks for this post! Nice clean and easy.

 GaB

Great tips thanks!

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

 Callie

Wow, that helped alot. Thank you!

 Devin S.

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.

 seutje

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

 Urban

Good good tnx :D

 Kyle

This is brilliant, thanks very much!

 Ash

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

 LeWebDev

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

You shouldn’t use it.

 Djela

simple, elegant and so useful.
thanks for sharing.

 matt

awesome, thanks.

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

 IAtect

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

 asdasdasdasd

exactly what i needed.. thanks!

 Skinny

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

 Skinny

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.

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 :)

 Qvr

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′)”

 Skinny

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

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

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

 Marinus

So many people, so many wishes…

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

Great! :D

 aris

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.

 j. kouri

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

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

 Ricky

Effortlessssss ……………..very good yar

 icooci

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

 James

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

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

 seda

thanks it worked!

sweet, I can even do it. Thanks!

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.

 adam

Cheers worked a treat

 Yuri

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

 smudgedlens

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…

 E.

Nice. Thanks! Worked on first try.

Leave a comment

WP_Big_City