Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a div into a link

I have a <div> block with some fancy visual content that I don't want to change. I want to make it a clickable link.

I'm looking for something like <a href="…"><div> … </div></a>, but that is valid XHTML 1.1.

like image 648
EE. Avatar asked Apr 28 '09 03:04

EE.


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.

How do I give a div a link in HTML?

You can make the entire DIV function as a link by adding an onclick="window. location='TARGET URL'" and by setting its style to "cursor:pointer". But it's often a bad idea to do this because search engines won't be able to follow the resulting link, readers won't be able to open in tabs or copy the link location, etc.

How do you make a whole div a clickable link?

We simply add the onlcick event and add a location to it. Then, additionally and optionally, we add a cursor: pointer to indicate to the user the div is clickable. This will make the whole div clickable.

How do I make a div block a link?

So we pushed an update to make that process less of a pain: now you can simply right click on a div block to convert it to a link block (and vice versa). Right-click to convert div blocks to link blocks (and vice versa).


2 Answers

Came here in the hope of finding a better solution that mine, but I don't like any of the ones on offer here. I think some of you have misunderstood the question. The OP wants to make a div full of content behave like a link. One example of this would be facebook ads - if you look, they're actually proper markup.

For me the no-nos are: javascript (shouldn't be needed just for a link, and very bad SEO/accessibility); invalid HTML.

In essence it's this:

  • Build your panel using normal CSS techniques and valid HTML.
  • Somewhere in there put a link that you want to be the default link if the user clicks on the panel (you can have other links too).
  • Inside that link, put an empty span tag (<span></span>, not <span /> - thanks @Campey)
  • give the panel position:relative
  • apply the following CSS to the empty span:

    {    position:absolute;    width:100%;   height:100%;   top:0;   left: 0;    z-index: 1;    /* fixes overlap error in IE7/8,       make sure you have an empty gif */   background-image: url('empty.gif'); }    

    It will now cover the panel, and as it's inside an <A> tag, it's a clickable link

  • give any other links inside the panel position:relative and a suitable z-index (>1) to bring them in front of the default span link
like image 122
thepeer Avatar answered Oct 19 '22 05:10

thepeer


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.

a {     display: block; } 

You can then set the width and height on it.

like image 26
Soviut Avatar answered Oct 19 '22 03:10

Soviut