简体   繁体   English

如何使导航栏中的列表项的整个区域可作为链接单击?

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

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 the text itself.我有一个由无序列表组成的水平导航栏,每个列表项都有很多填充以使其看起来不错,但唯一用作链接的区域是文本本身。 How can I enable the user to click anywhere in the list item to active the link?如何使用户能够单击列表项中的任意位置以激活链接?

 #nav { background-color: #181818; margin: 0px; overflow: hidden; } #nav img { float: left; padding: 5px 10px; margin-top: auto; margin-bottom: auto; vertical-align: bottom; } #nav ul { list-style-type: none; margin: 0px; background-color: #181818; float: left; } #nav li { display: block; float: left; padding: 25px 10px; } #nav li:hover { background-color: #785442; } #nav a { color: white; font-family: Helvetica, Arial, sans-serif; text-decoration: none; }
 <div id="nav"> <img src="/images/renderedicon.png" alt="Icon" height="57" width="57" /> <ul> <li><a href="#">One1</a></li> <li><a href="#">Two</a></li> <li><a href="#">Three</a></li> <li><a href="#">Four</a></li> </ul> </div> <div> <h2>Heading</h2> </div>

Don't put padding in the 'li' item.不要在 'li' 项中放置填充。 Instead set the anchor tag to display:inline-block;而是将锚标记设置为display:inline-block; and apply padding to it.并对其应用填充。

Define your anchor tag css property as:将您的锚标记 css 属性定义为:

{display:block}

Then the anchor will occupy the entire list area, so your click will work in the empty space next to your list.然后锚点将占据整个列表区域,因此您的单击将在列表旁边的空白区域工作。

Make the anchor tag contain the padding rather than the li .使锚标记包含填充而不是li This way, it will take up all the area.这样,它将占据所有区域。

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!) (警告:flexboxes 仍然没有得到很好的支持。Autoprefixer 来救援!)

Use following:使用以下:

a {
  display: list-item;
  list-style-type: none;
}

Or you could use jQuery:或者你可以使用 jQuery:

$("li").click(function(){
   window.location=$(this).find("a").attr("href"); 
   return false;
});

You should use this CSS property and value into your li :你应该在你的li使用这个 CSS 属性和值:

pointer-events:all;

So, you can handle the link with jQuery or JavaScript, or use an a tag, but all other tag elements inside the li should have the CSS property:因此,您可以使用 jQuery 或 JavaScript 处理链接,或使用a标签,但li所有其他标签元素都应具有 CSS 属性:

pointer-events:none;

Just simply apply the below css :只需简单地应用以下 css :

<style>
  #nav ul li {
    display: inline;
  }

  #nav ul li a {
    background: #fff;// custom background
    padding: 5px 10px;
  }
</style>

here is how I did it这是我如何做到的

Make the <a> inline-block and remove the padding from your <li>制作<a>内联块并从<li>删除填充

Then you can play with the width and the height of the <a> in the <li>然后你可以玩<li> <a>widthheight

Put the width to 100% as a start and see how it workswidth100%作为开始,看看它是如何工作的

PS:- Get the help of Chrome Developer Tools when changing the height and width PS:- 在更改heightwidth时获得 Chrome 开发者工具的帮助

If you have some constraint where you need to keep <li> structure as is and would like your a tag to take up the full area within the li element you can do the following:如果您有一些限制,您需要保持<li>结构原样,并且希望您的 a 标签占据 li 元素内的整个区域,您可以执行以下操作:

a {
    display: flex !important;
    width: -webkit-fill-available;
    height: -webkit-fill-available;
    justify-content: center;
    align-items: center;
}

You can always wrap the whole tag li in with the hiperlink tag Here is my solution: 你总是可以用hiperlink标签包装整个标签li这是我的解决方案:

 #nav { background-color: #181818; margin: 0px; overflow: hidden; } #nav img { float: left; padding: 5px 10px; margin-top: auto; margin-bottom: auto; vertical-align: bottom; } #nav ul { list-style-type: none; margin: 0px; background-color: #181818; float: left; } #nav li { display: block; text-align: center; float: left; width: 40px; padding: 25px 10px; } #nav li:hover { background-color: #785442; } #nav a { color: white; font-family: Helvetica, Arial, sans-serif; text-decoration: none; } 
 <div id="nav"> <img src="/images/renderedicon.png" alt="Icon" height="57" width="57" /> <ul> <a href="#"><li>One1</li></a> <a href="#"><li>Two</li></a> <a href="#"><li>Three</li></a> <a href="#"><li>Four</li></a> </ul> </div> <div> <h2>Heading</h2> </div> 

Put the list item within the hyperlink instead of the other way round.将列表项放在超链接中,而不是相反。

For example with your code:例如使用您的代码:

<a href="#"><li>One</li></a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM