Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make entire div a link? [duplicate]

Tags:

html

css

I have a div like this <div class="xyz"></div> and all the content in that div is in the css. How do I make that div into a link? I tried wrapping the a tag around it, but that didn't seem to work.

Thanks!!

like image 243
GeekedOut Avatar asked Feb 10 '12 13:02

GeekedOut


People also ask

Can you make an entire 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 whole card clickable?

You can wrap block elements with <a> . It is valid HTML5 and renders your card clickable. You may need to adjust the css rules for the text inside this block however.

How do you hyperlink a link in a div?

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 add a link to a div in HTML?

Create HTMLUse the <a> element to add the needed link.


2 Answers

You need to assign display: block; property to the wrapping anchor. Otherwise it won't wrap correctly.

<a style="display:block" href="http://justinbieber.com">   <div class="xyz">My div contents</div> </a> 
like image 173
Barry Chapman Avatar answered Nov 06 '22 05:11

Barry Chapman


Using

<a href="foo.html"><div class="xyz"></div></a> 

works in browsers, even though it violates current HTML specifications. It is permitted according to HTML5 drafts.

When you say that it does not work, you should explain exactly what you did (including jsfiddle code is a good idea), what you expected, and how the behavior different from your expectations.

It is unclear what you mean by “all the content in that div is in the css”, but I suppose it means that the content is really empty in HTML markup and you have CSS like

.xyz:before { content: "Hello world"; } 

The entire block is then clickable, with the content text looking like link text there. Isn’t this what you expected?

like image 33
Jukka K. Korpela Avatar answered Nov 06 '22 06:11

Jukka K. Korpela