Make whole
  • as link with proper HTML
  • 2019-03-13 01:03发布

    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!

    9条回答
    Juvenile、少年°
    2楼-- · 2019-03-13 01:41
    • 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/

    查看更多
    ゆ 、 Hurt°
    3楼-- · 2019-03-13 01:46

    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/

    查看更多
    再贱就再见
    4楼-- · 2019-03-13 01:48

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

    查看更多
    做个烂人
    5楼-- · 2019-03-13 01:48

    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

    查看更多
    叼着烟拽天下
    6楼-- · 2019-03-13 01:50

    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.

    查看更多
    贪生不怕死
    7楼-- · 2019-03-13 01:53

    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

    查看更多
    登录 后发表回答