160

I want to make it so that a whole div is clickable and links to another page when clicked without JavaScript and with valid code/markup.

If I have this which is what I want the result to do -

<a href="#">
<div>This is a link</div>
</a>

The W3C validator says that block elements shouldn't be placed inside an inline element. Is there a better way to do this?

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
Andy Lobel
  • 3,356
  • 9
  • 31
  • 40

12 Answers12

170

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>
Toto
  • 89,455
  • 62
  • 89
  • 125
Mario
  • 1,999
  • 2
  • 15
  • 28
  • 1
    See this page to make it work properly in Internet Explorer (basically just set the width or height to 1%: http://v3.thewatchmakerproject.com/journal/154/simple-css-how-to-make-clickable-areas-bigger – sventechie Jan 04 '13 at 17:20
  • I believe the 1% hack is needed only for <= IE6, possibly IE7. Anyone know for sure? – Scott Stafford Jul 11 '14 at 15:48
  • The `a` will not fill a `td` that has padding, and I assume the same is true for a `div`. I'm not sure if there's a way around it other than removing padding from the container. Negative margins on the `a` do not help. HTML4. `a` also shrinks to nothing if it is not surrounding anything. – Buttle Butkus May 06 '16 at 01:46
135
<div onclick="location.href='#';" style="cursor: pointer;">
</div>
Jamshid Hashimi
  • 7,639
  • 2
  • 28
  • 27
99

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)

xdevs23
  • 3,824
  • 3
  • 20
  • 33
Alex Norcliffe
  • 2,439
  • 2
  • 17
  • 21
24

Without JS, I am doing it like this:

My HTML:

<div class="container">
  <div class="sometext">Some text here</div>
  <div class="someothertext">Some other text here</div>
  <a href="#" class="mylink">text of my link</a>
</div>

My CSS:

.container{
  position: relative;
}

.container.a{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  text-indent: -9999px; //these two lines are to hide my actual link text.
  overflow: hidden; //these two lines are to hide my actual link text.
}
Tenzin Chemi
  • 5,101
  • 2
  • 27
  • 33
Sinan Erdem
  • 1,014
  • 1
  • 13
  • 22
11

My solution without JavaScript/images. Only CSS and HTML. It works in all browsers.

HTML:

<a class="add_to_cart" href="https://www.example.com" title="Add to Cart!">
  buy now<br />free shipping<br />no further costs
</a>

CSS:

.add_to_cart:hover {
  background-color:#FF9933;
  text-decoration:none;
  color:#FFFFFF;
}

.add_to_cart {
  cursor:pointer;
  background-color:#EC5500;
  display:block;
  text-align:center;
  margin-top:8px;
  width:90px;
  height:31px;
  border-radius:5px;
  border-width:1px;
  border-style:solid;
  border-color:#E70000;
}
John
  • 499
  • 12
  • 20
10

Nesting block level elements in anchors is not invalid anymore in HTML5. See http://html5doctor.com/block-level-links-in-html-5/ and http://www.w3.org/TR/html5/the-a-element.html. I'm not saying you should use it, but in HTML5 it's fine to use <a href="#"><div></div></a>.

The accepted answer is otherwise the best one. Using JavaScript like others suggested is also bad because it would make the "link" inaccessible (to users without JavaScript, which includes search engines and others).

selfthinker
  • 554
  • 1
  • 5
  • 15
4
.clickable {
  cursor:pointer;
}
Bruno
  • 6,211
  • 16
  • 69
  • 104
3

jQuery would allow you to do that.

Look up the click() function: http://api.jquery.com/click/

Example:

$('#yourDIV').click(function() {
  alert('You clicked the DIV.');
});
Josh Foskett
  • 4,073
  • 5
  • 31
  • 35
3

Well you could either add <a></a> tags and place the div inside it, adding an href if you want the div to act as a link. Or else just use Javascript and define an 'OnClick' function. But from the limited information provided, it's a bit hard to determine what the context of your problem is.

Dot NET
  • 4,891
  • 13
  • 55
  • 98
  • i already use this but it sais your not aloud to put block elements inside inline elements so was tryna find a good way to do it (: – Andy Lobel Nov 17 '11 at 00:18
  • OP asked for "valid code" (I assume they meant valid *markup*) - nesting a tags inside divs is not valid. – Alex Norcliffe Nov 17 '11 at 00:35
  • 1
    As has been stated on other answers above, divs inside a tags are now valid html5. http://w3c.github.io/html-reference/a.html#a-changes – redfox05 Feb 25 '16 at 15:31
2

Something like this?

<div onclick="alert('test');">

</div>
Tys
  • 3,592
  • 9
  • 49
  • 71
0

we are using like this

     <label for="1">
<div class="options">
<input type="radio" name="mem" id="1" value="1" checked="checked"/>option one
    </div>
</label>
   <label for="2"> 
<div class="options">
 <input type="radio" name="mem" id="2" value="1" checked="checked"/>option two
</div></label>

using

  <label for="1">

tag and catching is with

id=1

hope this helps.

0

AFAIK you will need at least a little bit of JavaScript...

I would suggest to use jQuery.

You can include this library in one line. And then you can access your div with

$('div').click(function(){
  // do stuff here
});

and respond to the click event.