下拉菜单链接不起作用
我构建了一个简单的列表并向其中添加了 css。现在垂直菜单可以工作了..问题出在 css 部分。列表项区域比链接本身大。这意味着,如果用户单击该区域,则不会发生任何事情,因为链接区域未覆盖所有列表项区域。
#sidebar1 li {
list-style: none;
position: relative;
width: 120px;
height: 30px;
padding: 0 20px;
background-color: black;
line-height: 30px;
cursor: pointer;
}
#sidebar1 li a {
text-decoration: none;
color: white;
}
我想做的是将链接填充或宽度与列表宽度相匹配。因此,无论用户何时单击菜单项,都会单击一个链接。这就是问题所在,我尝试过但没有成功
I build a simple list and added to it css. Now the vertical menu works.. the problem is in the section of the css. The list items area is bigger than the links themselves. That means that if the user clicks on the area, nothing happens cause the links area doesnt cover all the lists items area.
#sidebar1 li {
list-style: none;
position: relative;
width: 120px;
height: 30px;
padding: 0 20px;
background-color: black;
line-height: 30px;
cursor: pointer;
}
#sidebar1 li a {
text-decoration: none;
color: white;
}
What I thought to do was to match the links padding or width to that of the lists width. So wherever the users clicks on the menu's item a link will be clicked. Thats problem is that i tried it and it didnt work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将大部分样式移至 A 标签并修复一些问题:
Move most of the styling to the A-tag and fix a few things:
只需使用
display: block
使a
元素填充父元素的可用水平宽度:height: 100%
强制a
元素填充父元素的可用水平宽度: >a 继承父元素的完整高度。删除父li
中的内边距,否则您将在a
和li
的边缘之间强制留出空格。此外,在您的
li
中,我不仅删除了padding
(这只会导致如上所述的问题),还删除了cursor:pointer
,就像用户将鼠标悬停在链接上一样,光标会自动更改,如果用户不在链接上,则光标的类型(指针的类型)在单击时不会产生任何效果,这只是令人困惑:Just use
display: block
to make thea
element fill the available horizontal width of the parent element:The
height: 100%
forces thea
to inherit the full height of the parent element. Remove padding from the parentli
, otherwise you'll enforce a space between the edges of thea
and theli
.Further, in your
li
I've not only removed thepadding
(which simply causes problems as noted above), but also thecursor: pointer
, as if the user hovers over the link the cursor will change automatically, if they're not over the link then the cursor's type, that ofpointer
, is merely confusing when clicking produces no effect: