Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entire div as a link?

Tags:

html

I want to use an entire div as a link.. without the use of "onclick"

Is this possible with a simple href?

Thanks

like image 813
Latox Avatar asked Jan 13 '11 07:01

Latox


People also ask

Can you make a whole div 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.

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

Can you make a div clickable?

The answer is definitely yes, but you will need to write javascript code for it. We can use a click handler on the div element to make it clickable.

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;" ...


3 Answers

No, div elements cannot have the href attribute, nor will they act like links. You can use the following in HTML5:

<a href="#"><div></div></a>

However, not all current generation browsers (notably Firefox 3.6) supports this. The other alternative is to add a absolutely positioned a element that has 100% width and height to the div:

div a {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
}

This is valid HTML4, but it won't work on older versions of IE, of course. Otherwise should accomplish what you want. See: http://www.jsfiddle.net/uRh7j/

like image 176
Yi Jiang Avatar answered Oct 30 '22 08:10

Yi Jiang


you can't. but you could use an <a> with style="display:block", wich should behave exactly like a <div>, to replace your <div>

like image 35
oezi Avatar answered Oct 30 '22 06:10

oezi


simple answer is no, you can use onclick with css cursor:pointer to get the same functionality, though.

like image 33
Will Avatar answered Oct 30 '22 08:10

Will