如何使导航栏中列表项的整个区域可作为链接单击?
我有一个由无序列表制成的水平导航栏,每个列表项都有很多填充以使其看起来不错,但唯一可用作链接的区域是文本本身。如何使用户能够单击列表项中的任意位置来激活链接?
#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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
将锚标记 css 属性定义为:
然后锚将占据整个列表区域,因此您的单击将在列表旁边的空白区域中起作用。
Define your anchor tag css property as:
Then the anchor will occupy the entire list area, so your click will work in the empty space next to your list.
使锚标记包含填充而不是
li
。这样,它将占据所有区域。Make the anchor tag contain the padding rather than the
li
. This way, it will take up all the area.超级超级晚了,但无论如何:您也可以将锚点设置为弹性项目。这对于动态调整大小/排列的列表项特别有用。
(警告:flexboxes 仍然没有得到很好的支持。Autoprefixer 来救援!)
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.
(Caveat: flexboxes are obvs still not well supported. Autoprefixer to the rescue!)
使用以下:
Use following:
或者你可以使用 jQuery:
Or you could use jQuery:
您应该在
li
中使用此 CSS 属性和值:因此,您可以使用 jQuery 或 JavaScript 处理链接,或者使用
a
标记,但内部的所有其他标记元素li
应该具有 CSS 属性:You should use this CSS property and value into your
li
:So, you can handle the link with jQuery or JavaScript, or use an
a
tag, but all other tag elements inside theli
should have the CSS property:只需简单地应用下面的 css :
Just simply apply the below css :
这是我的做法
制作
内联块并从
中删除填充
然后您可以使用
width< /code> 和
中
的
height
将
width
改为以100%
作为开始,看看它是如何工作的PS:- 在更改
height
和width
时获取 Chrome 开发者工具的帮助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 theheight
of the<a>
in the<li>
Put the
width
to100%
as a start and see how it worksPS:- Get the help of Chrome Developer Tools when changing the
height
andwidth
如果您有一些限制,需要保持
结构不变,并且希望 a 标签占据 li 元素内的整个区域,您可以执行以下操作:
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:将列表项放在超链接内,而不是相反。
例如您的代码:
Put the list item within the hyperlink instead of the other way round.
For example with your code:
不要在“li”项中放置填充。相反,将锚标记设置为
display:inline-block;
并对其应用填充。Don't put padding in the 'li' item. Instead set the anchor tag to
display:inline-block;
and apply padding to it.