HTML - how to make an entire DIV a hyperlink?
Solution 1
You can add the onclick for JavaScript into the div.
<div onclick="location.href='newurl.html';"> </div>
EDIT: for new window
<div onclick="window.open('newurl.html','mywindow');" style="cursor: pointer;"> </div>
Solution 2
You can put an <a>
element inside the <div>
and set it to display: block
and height: 100%
.
Solution 3
You just need to specify the cursor as a pointer, not a hand, as pointer is now the standard, so, here's the example page code:
<div onclick="location.href='portable-display-stands.html';" id="smallbox">The content of the div here</div>
and the example CSS:
#smallbox {
cursor: pointer;
}
So the div is now a clickable element using 'onclick' and you've faked the hand cursor with the CSS...job done, works for me!
Solution 4
This is a late answer, but this question appears highly on search results so it's worth answering properly.
Basically, you shouldn't be trying to make a div clickable, but rather make an anchor div-like by giving the <a>
tag a display: block
CSS attribute.
That way, your HTML remains semantically valid and you can inherit the typical browser behaviours for hyperlinks. It also works even if javascript is disabled / js resources don't load.
Solution 5
Add an onclick
to your DIV
tag.
Admin
Updated on August 18, 2020Comments
-
Admin almost 4 years
How do I make an entire
DIV
a clickable hyperlink. Meaning, I essentially want to do:<div class="myclass" href="example.com"> <div>...</div> <table><tr>..</tr></table> .... </div>
And whenever someone mouse hovers of the myclass
DIV
, I want the entireDIV
it to be a clickable hyperlink. -
Admin over 14 yearsThat won't work because I have a lot of content inside the DIV. I'll update my original post to make it more descriptive
-
prodigitalson over 14 years@Teddy: Adding
position:relative;
to thediv
and thenposition: absolute; height: 100%;
to thea
as well... should make the entire area of thediv
is active. -
Michael Itzoe over 14 yearsAlso, set the div's cursor style to pointer so the whole thing gets the finger.
-
SLaks over 14 yearsYou can put all of that content inside the
<a>
. -
SLaks over 14 yearsDemo: jsbin.com/adoka
-
Admin over 14 yearsWhen I do that, my mouse cursor doesn't change to a hand like it does when hovering over a link. What am I doing wrong?
-
Admin over 14 yearsWhen I do that, my mouse cursor doesn't change to a hand like it does when hovering over a link. What am I doing wrong?
-
Keith Adler over 14 yearsThe sample at the link shows that setting as well.
-
Joel Etherton over 14 yearsYou would need to use window.open. There are many canned scripts that perform this action. javascript-coder.com/window-popup/javascript-window-open.phtml
-
Admin over 14 yearsif you modified the move to be, cursor:pointer - that works
-
Andrew Moore over 14 yearsThat is not true.
<a>
is an inline level element capable of holding only inline level elements. Which means that having<div>
and/or<table>
has children of an<a>
is not valid XHTML. To workaround this limitation, use inline elements and CSS to display them as block level elements. See cs.sfu.ca/CC/165/common/ref/wdgxhtml10/inline.html for a list of inline elements. -
Dhawal Sawla over 14 yearsonclick actually does not make it a link but just clickable. You could use the css cursor attribute to change it likewise.
-
Christopher Parker over 14 yearsFYI: If the
<a>
were made an overlay, as prodigitalson suggested, then none of the elements "behind" the<a>
would be selectable. -
Ahmad Alfy over 11 yearsThis is valid HTML5 DOCTYPE only ... Wont validate if you're using HTML4 or XHTML
-
Wayne Werner over 11 yearsThis worked perfectly for me with
<td><a...></a></td>
-
b1r3k over 10 yearsworks like a charm if you cannot use javascript solution presented above!
-
davidcondrey over 10 yearsYou need to make the anchor a block for this to work.
-
bazzilic over 9 yearsUse
cursor: pointer
instead ofcursor: hand
. -
Richard Moore over 8 yearsThat wouldn't be very good for SEO. Search engines wouldn't see the hyperlink if it was coded in JavaScript.
-
JohnGIS almost 5 yearsThis is exactly what I needed, thanks!
-
danielnixon almost 5 years
-
danielnixon almost 5 years
-
danielnixon almost 5 years
-
danielnixon almost 5 years
-
Joel Etherton over 4 years@danielnixon: That's a fantastic resource and something any developer should consider when creating a user interface of any kind through the web. This question didn't have any such considerations and asked only "how to do it". Perhaps your comment is best suited at the question level because it is certainly relevant.