Update November 2020: I think the best possible technique for this is Method 4 in this article. The <div>
(or whatever wrapper element) remains semantic and accessible, while being “clickable” over the whole area. It doesn’t break text selection and respects other “nested” interactive elements.
This is perfectly valid HTML:
<a href="http://example.com">
<div>
anything
</div>
</a>
And remember you can make links display: block;
, so the whole rectangular area becomes “clickable”. But, if there is a ton of content in there, it’s absolutely horrible for accessibility, reading all that content as the interactive link.
If you absolutely need to use JavaScript, one way is to find a link inside the div and go to its href
when the div is clicked. This is with jQuery:
$(".myBox").click(function() {
window.location = $(this).find("a").attr("href");
return false;
});
Looks for a link inside div with class of “myBox”. Redirects to that links value when anywhere in div is clicked.
Reference HTML:
<div class="myBox">
blah blah blah.
<a href="http://google.com">link</a>
</div>
Or, you could set a data-*
attribute on the <div data-location="http://place.com">
and do like:
window.location = $(".myBox").data("location");
works very nicely. thanks.
I created a Jquery plugin to do this. Check this: http://plugins.jquery.com/project/divlinker
Best solution for a clickable div. Thank you.
btw @Leandro, Link is not working :(
You are my hero, this worked perfectly!
Thanks for the HTML5 update! Saved me from doing something dumb.
Chris, thanks for this. When I tried this it worked for a hover over empty space in the div but not for the link itself; when I clicked on the link itself it went to a 404 error. Any ideas?
It will make any div with the .myBox class clickable, so if there’s no link in one, it’ll 404, is that the problem?
Very very nice.
I had a job interview today. When asked if there were any blogs I read regularly, I mentioned css-tricks, and the interviewer said that was one of his faves, too.
I love to hear that!
Great technique.
What if you want to have other links inside the linked div? How would you keep those working still?
You could create a variable for the exact element that you want to target. Question is though, why would you want to do that? If you could control the dom just do it there. This is more for a last resort type deal like using it on something like Drupal Core links.
this is very useful for my current project
“window.location” seems not to work in IE if you are using RealURL. Any Ideas?
Thx
Christoph
fixed it this way:
I think i am going to try this with tables…. Or is there an easier way?
did it work on thie table.
wow … very sweet idea :- )
thanks
Nice work
really helpful
There’s a pure CSS way to go about doing this. Just have an anchor (a) tag within the DIV that links to the URL you want. Then, set it to a block display, relative positioning, left and top equal to 0, width and height to full (or use jQuery to match all A’s within DIV’s to the same dimensions as the DIV).
Great Solution! This really helped.
Awesome… this helped me alot!
great tip but it should be absolute position not relative position.
so set the div to relative position, and the “a” tag to
display:block; absolute; top:0; left:0; width: 300px; height:200px;
Don’t listen to the person above me.
They will make your ENTIRE page a link…
Relative will make your “top:0; left:0;” relative to the DIV in which it’s contained, rather than absolute, which makes it top / left of the PAGE, not just DIV.
You’re welcome :)
This method works btw, but will interact differently depending on what other text / images / etc. you have in the same div, since it’s block display, so make sure you know what you want where prior to setting this up.
Absolute positioning will be relative to the first parent element that has positioning set. In dan’s post he said to set the div’s position to relative and then make the link’s position absolute. This means that the top:0; left:0; will be referencing the containing div not the body of the page. If the position was not set on the parent div or any other ancestor elements, then it would take up the whole page as you pointed out.
That being said I’m not sure if it is better to use absolute or relative positioning for the links, or if there is even a significant difference.
Michael’s answer is not entirely correct, and Malcolm did a good job of explaining why.
I, too, am wondering whether absolute/relative positioning is even necessary here. I’ve successfully made entire divs clickable without messing with the positioning of the links. Hmmm I’ll have to look into this.
I believe Dan’s answer to be correct, and sadly to float this over content out of the flow of text & images requires an absolute positioning (within a relative positioned parent element).
Micheal’s comment appears to be confusing ‘absolute’ with ‘fixed’ which automatically uses the edge of the browser chrome for positioning.
however support for css3 positioning is less reliable than the ubiquitous jQuery currently.
Yes. Yes yes yes. Delightful. Thanks, TerranRich.
Very nice solution.
I agree with above who say that absolute position is “relative” to the parent container if the parent container position is different from the default one (static).
Anyway, very very nice, thanks.
This is awesome. I’ve been trying to figure out a better way to do this for a while.
How can I ensure that the href title appears when I hover on the div and also that the URL appears in the status bar of the browser I’m using?
Very nice thanks :D
fasf
Its much nicer this way and not having to wrap a whole div in an A tag.
For usability (dependent on what you are using it for), remember to add cursor:pointer in your css. Nice simple bit of code thanks.
Hi,
I want to make entire div clickable, but my div is consisting of news ticker and each of then linked to different URL, will this solution work in these situation?
Thanks,
Dearthelusion
How about opening in a new window?
oops, didn’t mean that to be a direct reply =]
I’m new to scripting in jQuery. How would I set the target to be a new window?
Any help would be greatly appreciated!
@l3v1tcl30s
So how do you combine that with Chris’ code? I’ve tried all sorts of combinations, like:
but I can’t get it to work
This is not working:
As Mike was asking, Anyway to add target=”_blank” to roy post original jquery code?
Thanks in advance.
This doesn’t seem to work with IE 8.0. Is that correct?
if the div consist of more than 1 hyperlink. which url will the jquery reads
Yeh roy! Thanks!
Quick and lightweight, thanks! You can also wrap an bunch of elements in “a” tags, and this is semantic HTML5 markup. But it’ve had issues with that in older browsers. If you were to go that route make sure all the elements inside the “a” are inline by default (like a span or p), if necessary you can change them to display: block in the CSS.
Chris’ method seems a little cleaner tho ;)
Oh, thanks! It’s the solution I’ve beem looking for!
Nice tricks Thanks!
Why not just do this:
This is really simple and it works pretty well.
Mr John, your idea is very simple & working fine for all browser types :)
Seems like this is an old thread, but hey it was still useful to me!
Also, thanks to the person above me (John), I actually used your solution.
Regards
What would be the best practice to apply this into a Joomla! Website?
I found this method and I wanted to share it with you guys because I found it to be just want I needed for a DIV that contains more then one link:
You can even use this for more then one div if the window.location is the same for both of them like this:
Cool solution, but in case someone is googling around for a solution (like I was today), I found a thread over in stackoverflow for a solution sans js:
http://stackoverflow.com/questions/796087/make-a-div-into-a-link
For those looking for valid HTML way without javascript.
cheers!
Stunning simple solution.
And by adding a tiny bit of jquery, you get all links you want opening in a new window:
http://jsfiddle.net/hf75B/81/
Good search Tak, thx a lot.
The solution gets even simpler…no need for jquery sizing or any of that business:
div {
position: relative;
}
a {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
}
Or by inserting the following within your div tag:
onclick=”location.href=’http://www.example.com’;” style=”cursor:pointer;
For those using HTML 5 you can now wrap any block-level element in an A tag:
<a href=”#”>
<div>
blah blah blah
</div>
</a>
And for hover state just add a class to your div, and then add a css class .whatever:hover {}.
This is a javascript trick…not a CSS trick.
thanks man, this works like magic
hi there it is what I got to get 2 div with images bg clickable
I am just looking for the solution to get it open in a new window target:”_blank”
can someone help me on this as the info up above are not working for me
I’m sorry, but any solution that involves javascript is complete overkill. This can be accomplished quite simply using HTML and CSS alone:
HTML CODE:
CSS CODE:
Just specify the width and height of the clickable area.
Less lines of code is always better.
I use javascript only when necessary. Remember that 3% of users have it disabled on their browsers.
Or you can simply add this inside your div tag:
onclick=”location.href=’http://www.example.com’;” style=”cursor:pointer;
Fewest lines as possible!
Hi,
Anyway to add
target="_blank"
to this:Simply adding:
as a new function within the jquery chunk is not working for me.
Thanks in advance.
This is really cool. But how to make whole not clickable.
I mean whole li not div clickable
Here is a much easier way:
Insert this within the DIV tag that you want to make linkable:
onclick=”location.href=’http://www.example.com’;” style=”cursor:pointer;
That’s all you have to do.
Thanks Chris, I just used this on the homepage of my site and added:
.myBox { cursor: hand; cursor: pointer; }
to force a hand cursor when rolling over the .
Out of interest, why not just use jQuery to trigger the click event on the link?
To make the link open in a new window/new tab, use the following code:
Halleluleja you saved my day …
After many answers on many sites including stackoverflow, this thread actually had working solutions. Thanks to all.
I’ve got a clickable div. With the HTML5 data- attribute I declare the link
f.e. <div class=”item” data-href=”LINK”></div>
In this div there is a link. If I click the link the page go to the data-href link.
But can you trigger that if you click the link in the text that you don’t go to the link that is specified in the data-href?
example on jsfiddle
The javascript is a bit of an overkill… An easier solution would be to wrap the div in a “link” with a display:block style
Then style your CSS to remove the underline or whatever.. Hope this helps.. :)
Adam, this solution works like a charm. Thanks.
Adam, Jaret,
I think this is valid HTML 5 but is it valid HTML 4?
Adam,
works like a charm. Beautifully simple, and effective. Thanks for passing along.
It works, but it’s very bad for SEO. The method of Adam is way better.
Better if you don’t have to worry about IExplorer8 and lower, and even better if you remove the inline css rule…
you can add functionality to this to allow you to insert subsequent links from the first by inserting and else statement and inserting this:
else{
window.location=e.attr(“href”);
}
Can’t recall where I found this or I’d give credit, but this is what works for me.
div
{
position: relative;
}
I have used this method,and it’s working very well.
Nice Giancarlo,
Unfortunately, doesnt that prevent me from making a background color change on hover?
I’ve read through all of the above. What’s the verdict that works in IE7 and above and allows a background color change on hover? I’m confused. The original doesn’t?
Nice trick, but we can simplified to this:
jQuery:
The above will create a square click-able div, the only annoying think here is if you clicked on it, on the location bar you will see
#
added to the end of address, to get over this, replace#
indata-href
attribute of the div withjavascript:;
. Tested on FireFox 19.Great! Fast and clean solution!
Hey
I get an error when using this in my wordpress theme, any suggestions?
unexpected ‘(‘, expecting variable (T_VARIABLE) or ‘$’
In my case I require the function validates whether links are external or not. This what I use. Works fine
@Liz (https://css-tricks.com/snippets/jquery/make-entire-div-clickable/#comment-454372),
THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU!!!
OMG i’ve spent hours (too many) looking for just this perfect snippet. much appreciated.
That is astounding, using such a simple technique to get great results.
This is a simple question I suppose. Say I’m using this to convert a number of equally named divs with this function and I want to turn one of them off if a certain image attribute exists. How would I remove this from a single div?
Thanks!
I LOVE CSS-TRICKSDOTCOM!!!
Very nice! Works like a charm!!
But using target “_blank” – its not working
Any ideas?
Cheers
sorry I think you got the wrong end of the stick.
Its an small symbol image – Hopefully this can open on another page when clicked.
The click is working – fine
For opening the link in the new window, instead of going with window.location = someurl say window.open([someurl])
thanks for this beautiful idea and nice trick.
I have one question, I hope your can solve this as well.
I am using CPC network ads on my website as publisher.
normally these website give you script to be placed inside the .
I have placed the code like this
<div class="someclass"> <script type="text/javascript" src="URL"></script></div>
Every time page is loaded a new URL is generated for the add.
I want to capture that URL and store it in a NEW-LINK
<a href="NEW-LINK"> LINK </a>
so that when i click this NEW-LINK , then it clicks the URL generated by the CPC script.
I have tried using iframes, but to no avail.
Thanks for your support
years later and still helping out.
thanks for this beautiful idea and nice trick.
Hi
I have created a clickable div tag,by Just using a piece of CSS code and no Javascript!
here is the solution:
http://jsfiddle.net/chriscoyier/y7L9D/
You would need
Because every body put their code :)
here is my solution :
Awesome snippet. I’ve modified it just slightly to support opening in a new or the same tab depending on your HTML.
After that, all you have to do is add the
click-box
class to yourdiv
, and include the link with thebox-link
class. Then you can set yourtarget
as_self
or_blank
depending on how you want that link to behave.Other one is add onclick=”location.href=’#’;” style=”cursor: pointer in DIV
What if I have multiple Divs? Do you think that this code will work? Perhaps every div must have a unique id and unique js code. Can’t we just merge it?
Is there any HTML5 solution for this? JavaScript in huge websites just takes lots of time to load even if it’s placed in bottom.
<div>
<a href="#" taregt="_blank">
<div>
</div>
</a>
</div>
Instead make a tag to display:block…
Dude. THANKS!
Great and simple.. Thanks saved the day
I couldn’t agree more with Chris’ opening suggestion of just nesting your div within an Anchor tag. Way more friendly to update and the less JS for UI I can do the better IMHO. An anchor tag is meant to be a link, so much better for screen readers too.
I initially wrote something like this, but it just seems to ugly to me.
Your second solution might be ugly, but it was perfect for what I needed to do in a site I’m working on right now. Thank you! =)
The html5 technique unfortunately does not work with ModX. ModX deletes the
<a>
tag automatically when saving.Bummer.
From my understanding, it’s not common practice to put divs within anchors. Is it now acceptable to do this, because I’ve heard both parties. One side is saying it’s not a good idea, the other side says it’s fine as long as there are no interactive items within the div that is being anchored.
Some clarification on this matter would be helpful, thanks.
Thanks for this! I used the jQuery method, works flawlessly :)
Thank you! I’m working on an old project and I’m not able to use HTML5. I used the jQuery method and it worked!
Could someone tell me what this window.location is doing in this code? How could this affect only a single div when we’re setting the location of the window object? I checked the doc and couldn’t find out.
Sorry for asking, now I have my answer, when window.location is set, it redirects the page to the assigned location, so when the div is clicked it works like putting a div inside an anchor tag (HTML5).
Hey there!
window.location
locates the area of the object it is assigned to. In this case, it is scoped to$(this)
which is the.myBox
class. Hope this helps!Why does this have to return false?
Hey there, quick question. Do you mind sharing which blog platform you’re using and would you recommend it?
I’m goinng to start my own blog (I’m hoping to build
up a reputation soon) but I’m having a tough tie deciding amongst
Squarespace/Wordpress/Wix/Tumblr and Drupal. What do you
suggest?
WordPress all the way.
https://mediatemple.net/blog/news/css-tricks-is-a-poster-child-wordpress-site/
But, like anything else, pick the right tool for the job and what makes you most compfortable.
hello i’m creating a website and i want to add clickable box container in it.
and i’m using atom. what should i write to add box and what should i do in css section?
For SEO reasons I would use the jQuery which finds the target link inside the myBox, just to be sure that the search engine bots find the links.
Why did you return false
A problem can arise when you have two anchor tags in one div
How could this be modified to handle default browser behavior for middle-mouse-click and ctrl+click? This usually opens the url in a new tab.
i meant wht if i got 2 a in that div
I’ve always used css-tricks but I’m using it more and more every day thanks to these little details, like someone having the foresight to add this November 2020 update note on this older article. Much appreciated!
It helped me to add link functionality to angular’s material2-carousel slide images like a charm!
<button>
<div>
</div>
<button>
This is semantically incorrect can someone correct it?