Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to make a clickable <div> button

Tags:

html

css

Kindly go through this link and notice the solution given by "thepeer": https://stackoverflow.com/a/3494108

I am having trouble understanding his solution. Let me illustrate with the example of a webpage that I am trying to build. I am using HTML5 to build "buttons". Now since these buttons are actually DIV objects, I somehow need to make them "clickable" which would lead the viewer to another page, and that too, without making the "link" visible. The div object should act as a button and I don't intend to "include" a hypertext-ed line of text inside the button.

I think the solution given by "thepeer" does exactly that but i am unable to understand his solution and implement it. Perhaps a very small example would benefit me.

Suppose this is the button that i want to make :

 <div id="music" class="nav">Music I Like <span id="hyperspan"><a href="Music.html"></a></span></div> 

And here's the CSS as "thepeer" suggested :

.hyperspan {     position:absolute;     width:100%;     height:100%;     left:0;     top:0;     z-index:1; } 

Now, obviously, I misunderstood his solution and hence my above attempt is incorrect. I don't want to resort to javascript so please help me! Sorry for my noobish-ness.

like image 728
finitenessofinfinity Avatar asked Dec 28 '11 19:12

finitenessofinfinity


People also ask

How do I make a div clickable?

Create CSSSet the position to "absolute" for the inner <a> tag. Use the z-index property to place the link above all the other elements in the div.

How do I make a div act like a button?

role=“button” ARIA (Accessible Rich Internet Applications) attributes provide the means to make a div appear as a button to a screen reader. Here is the intro to the button role page on MDN: Adding role=“button” will make an element appear as a button control to a screen reader.

How do I make a div clickable link in HTML?

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.

Can you put a button in a div?

Use the following steps to center align a button in a div container: Create a div container. Insert the button tag. In the CSS for the div set the text-align to center.


1 Answers

There are two solutions posted on that page. The one with lower votes I would recommend if possible.

If you are using HTML5 then it is perfectly valid to put a div inside of a. As long as the div doesn't also contain some other specific elements like other link tags.

<a href="Music.html">   <div id="music" class="nav">     Music I Like   </div> </a> 

The solution you are confused about actually makes the link as big as its container div. To make it work in your example you just need to add position: relative to your div. You also have a small syntax error which is that you have given the span a class instead of an id. You also need to put your span inside the link because that is what the user is clicking on. I don't think you need the z-index at all from that example.

div { position: relative; } .hyperspan {     position:absolute;     width:100%;     height:100%;     left:0;     top:0; }  <div id="music" class="nav">Music I Like      <a href="http://www.google.com">          <span class="hyperspan"></span>     </a>    </div> 

http://jsfiddle.net/rBKXM/9

When you give absolute positioning to an element it bases its location and size after the first parent it finds that is relatively positioned. If none, then it uses the document. By adding relative to the parent div you tell the span to only be as big as that.

like image 95
mrtsherman Avatar answered Sep 30 '22 06:09

mrtsherman