Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make whole <li> as link with proper HTML

I know this has been up a lot of times before, but I couldn't find any solution in my specific case.

I've got a navigation bar and I want the whole <li>'s to be "linked" or "clickable" if you will. Now only the <a> (and the <div>'s I've fiddled with) is "clickable".

I've tried the li a {display: inner-block; height: 100%; width: 100%} method but the results where just horrible.

The source can be found here: http://jsfiddle.net/prplxr/BrcZK/

HTML

<!DOCTYPE html>
<html>
    <head>
        <title>asdf</title>
    </head>
    <body>
        <div id="wrapper">
            <div id="menu">
                <div id="innermenu">    
                    <ul id="menulist">       
                        <li class="menuitem"><a href="index.php"><div class="menulink">Lnk1</div></a></li>
                        <li class="menuitem"><a href="index.php"><div class="menulink">Lnk2</div></a></li>
                        <li class="menuitem"><a href="index.php"><div class="menulink">Lnk3</div></a></li>
                        <li class="menuitem"><a href="index.php"><div class="menulink">Lnk4</div></a></li>
                    </ul>
                </div>
            </div>
        </div>
    </body>
</html>

Do anyone have a neat solution to this?

Thank you in advance!

like image 665
Christian Lundahl Avatar asked Dec 06 '12 14:12

Christian Lundahl


People also ask

How do you make a whole div A 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.

How do I make hyperlinks the same page in HTML?

Using #top or #bottom The following examples use #top and #bottom with the <a> tag and href attribute to link to that section of the page. This method is similar to using "id," but you don't have to pick a specific element. Click "Top" or "Bottom" in the Results section to see what happens.


2 Answers

  • Get rid of the <div>s.
  • Set the <a> tags to have display: block
  • Move the padding from the <li> to the <a>.
  • The <li>s will need to be either floated or display: inline-block

Example: http://jsfiddle.net/8yQ57/

like image 179
Steve Pugh Avatar answered Oct 20 '22 19:10

Steve Pugh


Just use "display block" for link.

ul {
  display: block;
  list-style-type: none;
}

li {
  display: inline-block; /* or block with float left */
  /* margin HERE! */
}

li a {
  display: block;
  /* padding and border HERE! */
}

Here's the example http://jsfiddle.net/TWFwA/ :)

like image 39
starikovs Avatar answered Oct 20 '22 19:10

starikovs