How do I make the whole area of a list item in my navigation bar, clickable as a link?

前端 未结 10 1914
栀梦
栀梦 2020-11-27 13:50

I\'ve got a horizontal navigation bar made from an unordered list, and each list item has a lot of padding to make it look nice, but the only area that works as a link is th

相关标签:
10条回答
  • 2020-11-27 14:16

    Make the anchor tag contain the padding rather than the li. This way, it will take up all the area.

    0 讨论(0)
  • 2020-11-27 14:17

    Define your anchor tag css property as:

    {display:block}
    

    Then the anchor will occupy the entire list area, so your click will work in the empty space next to your list.

    0 讨论(0)
  • 2020-11-27 14:17

    here is how I did it

    Make the <a> inline-block and remove the padding from your <li>

    Then you can play with the width and the height of the <a> in the <li>

    Put the width to 100% as a start and see how it works

    PS:- Get the help of Chrome Developer Tools when changing the height and width

    0 讨论(0)
  • 2020-11-27 14:19

    Don't put padding in the 'li' item. Instead set the anchor tag to display:inline-block; and apply padding to it.

    0 讨论(0)
  • 2020-11-27 14:19

    Use following:

    a {
      display: list-item;
      list-style-type: none;
    }
    
    0 讨论(0)
  • 2020-11-27 14:20

    Super, super late to this party, but anyway: you can also style the anchor as a flex item. This is particularly useful for dynamically sized/arranged list items.

    a {
      /* This flexbox code stretches the link's clickable 
       * area to fit its parent block. */
      display:        flex;
      flex-grow:      1;
      flex-shrink:    1;
      justify-content: center;
    }
    

    (Caveat: flexboxes are obvs still not well supported. Autoprefixer to the rescue!)

    0 讨论(0)
提交回复
热议问题