How to make a whole 'div' clickable in html and css without JavaScript?
<div onclick="location.href='#';" style="cursor: pointer;">
</div>
It is possible to make a link fill the entire div which gives the appearance of making the div clickable.
CSS:
#my-div {
background-color: #f00;
width: 200px;
height: 200px;
}
a.fill-div {
display: block;
height: 100%;
width: 100%;
text-decoration: none;
}
HTML:
<div id="my-div">
<a href="#" class="fill-div"></a>
</div>
a whole div links to another page when clicked without javascript and with valid code, is this possible?
Pedantic answer: No.
As you've already put on another comment, it's invalid to nest a div
inside an a
tag.
However, there's nothing preventing you from making your a
tag behave very similarly to a div
, with the exception that you cannot nest other block tags inside it. If it suits your markup, set display:block
on your a
tag and size / float it however you like.
If you renege on your question's premise that you need to avoid javascript, as others have pointed our you can use the onClick event handler. jQuery is a popular choice for making this easy and maintainable.
Update:
In HTML5, placing a <div>
inside an <a>
is valid.
See http://dev.w3.org/html5/markup/a.html#a-changes (thanks Damien)