Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make a div tag into a link

Tags:

html

anchor

How do you make a div tag into a link. I would like my entire div to be clickable.

like image 853
Alan Avatar asked Nov 06 '09 02:11

Alan


People also ask

Can you turn a div into a link?

You can't make the div a link itself, but you can make an <a> tag act as a block , the same behaviour a <div> has. You can then set the width and height on it. However, this doesn't make a 'div' into a link. It makes a link into a block element.

How do you make a div a hyperlink?

You can put an <a> element inside the <div> and set it to display: block and height: 100% . @Teddy: Adding position:relative; to the div and then position: absolute; height: 100%; to the a as well... should make the entire area of the div is active. You can put all of that content inside the <a> .

How do you link a div in HTML?

By prepending your href with # , you can target an HTML element with a specific id attribute. For example, <a href="#footer"> will navigate to the <div id="footer"> within the same HTML document. This type of href is often used to navigate back to the top of the page.

How do you wrap a div in a link?

You can put an anchor tag inside of the divs... make it the last element in the divs, and style it to be display block, and to fill up 100% width and height (you may need to specify the height). Then have it "position: absolute;" and have the outermost div "position: relative;" ...


2 Answers

JS:

<div onclick="location.href='url'">content</div>

jQuery:

$("div").click(function(){
   window.location=$(this).find("a").attr("href"); return false;
});

Make sure to use cursor:pointer for these DIVs

like image 123
eozzy Avatar answered Oct 02 '22 13:10

eozzy


If you're using HTML5, as pointed in this other question, you can put your div inside a:

<a href="http://www.google.com"><div>Some content here</div></a>

Preffer this method as it makes clear in your structure that all the content is clickable, and where it's pointing.

like image 30
Rael Gugelmin Cunha Avatar answered Oct 02 '22 14:10

Rael Gugelmin Cunha