How to make a whole 'div' clickable in html and css without JavaScript?

HtmlCssClick

Html Problem Overview


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?

Html Solutions


Solution 1 - Html

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>

Solution 2 - Html

<div onclick="location.href='#';" style="cursor: pointer;">
</div>

Solution 3 - Html

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

Solution 4 - Html

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.
}

Solution 5 - Html

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;
}

Solution 6 - Html

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

Solution 7 - Html

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.');
});

Solution 8 - Html

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.

Solution 9 - Html

.clickable {
  cursor:pointer;
}

Solution 10 - Html

Something like this?

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

</div>

Solution 11 - Html

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.

Solution 12 - Html

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndy LobelView Question on Stackoverflow
Solution 1 - HtmlMarioView Answer on Stackoverflow
Solution 2 - HtmlJamshid HashimiView Answer on Stackoverflow
Solution 3 - HtmlAlex NorcliffeView Answer on Stackoverflow
Solution 4 - HtmlSinan ErdemView Answer on Stackoverflow
Solution 5 - HtmlJohnView Answer on Stackoverflow
Solution 6 - HtmlselfthinkerView Answer on Stackoverflow
Solution 7 - HtmlJosh FoskettView Answer on Stackoverflow
Solution 8 - HtmlDot NETView Answer on Stackoverflow
Solution 9 - HtmlBrunoView Answer on Stackoverflow
Solution 10 - HtmlTysView Answer on Stackoverflow
Solution 11 - HtmlSebastian Oberste-VorthView Answer on Stackoverflow
Solution 12 - Htmluser1642018View Answer on Stackoverflow