Christian Lundahl
Christian Lundahl

Reputation: 2060

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!

Upvotes: 22

Views: 63669

Answers (9)

Mitchell
Mitchell

Reputation: 111

You should make the a elements display:block or display:inline-block.

Upvotes: 0

starikovs
starikovs

Reputation: 3398

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/ :)

Upvotes: 9

Jamie Hutber
Jamie Hutber

Reputation: 28064

I myself just had this problem.

The answer couldn't be simpler:

<li class="menuitem"><a href="index.php"><div class="menulink">Lnk1</div></a></li>
Wrong:
.menuitem {
    list-style-type:        none;
    display:                 inline;
    margin-left:            5px;
    margin-right:            5px;
    font-family:            Georgia;
    font-size:                11px;
    background-color:        #c0dbf1;
    border:                 1px solid black;
    padding:                10px 20px 10px 20px;
}

Correct
.menuitem a {
    list-style-type:        none;
    display:                 block;
    margin-left:            5px;
    margin-right:            5px;
    font-family:            Georgia;
    font-size:                11px;
    background-color:        #c0dbf1;
    border:                 1px solid black;
    padding:                10px 20px 10px 20px;
}

in other words, you want to apply the css that the LI's had to the A element. Making sure that the A is a block line element

Upvotes: 4

Erik
Erik

Reputation: 935

Here's what I do:

a { display: block; }

Then style the anchors as I see fit.

Here's the fiddle: http://jsfiddle.net/erik_lopez/v4P5h/

Upvotes: 0

Sean
Sean

Reputation: 650

I think you may have meant inline-block, not inner-block:

li a {display: inline-block; height: 100%; width: 100%; }

Also, inline-block has its own set of problem with older IE browsers, and probably won't react how you'd expect.

Upvotes: 1

Steve Pugh
Steve Pugh

Reputation: 1008

  • 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/

Upvotes: 54

Jona
Jona

Reputation: 2147

Just format the a directly instead of the li with your styles.

I have alterd your fiddle http://jsfiddle.net/BrcZK/1/

Upvotes: 0

Scott S
Scott S

Reputation: 2746

Move the <a> tags so that they wrap the <li> tags. According to this answer, anchor tags must contain inline elements, and it looks like your <li>'s are inline, so this should be valid.

Upvotes: -3

Michael Antonius
Michael Antonius

Reputation: 972

a sir you use jquery ? if yes, you can try it :

$("li").click(function(){
   $(this).find('a').click();
});

or you could use it too as css:

li{
  position:relative;
}
li a {
  position:absolute;top:0px;left:0px;width:100%;height:100%;
}

Choose one ... Good luck

Upvotes: -1

Related Questions