如何消除链接<a>和所在列表项<li>之间的所有填充?或者...点击<li>会打开其中的链接?

3

我的代码:

<ul class='sub-menu'>
  <li><a href='something'>something</a></li>
  <li><a href='else'>else</a></li>
  <li><a href='another'>another</a></li>
</ul>

我可以点击列表项(li)的任何位置,链接不会打开(就像链接本身和它所在的列表项之间有填充一样)。我想能够点击列表项的任何部分,并打开其中的链接。
我一直在尝试使用内联CSS来强制实现我想要的行为,但仍然没有成功。请帮忙?

1
为什么这被标记为C#? - maccettura
2个回答

1
在锚点上创建一个伪元素,覆盖它们的列表项,包括标志符。
.sub-menu li {
  position: relative;  /* to contain the absolute-positioned pseudo-element */
}

.sub-menu a:before {
  content: '';         /* required for most pseudo-elements */
  position: absolute;  /* absolutely positioned */
  top: 0;              /* ... at top of its list item */
  left: -50px;         /* ... to the left of its list item, including the bullet */
  right: 0;            /* to its list item's right */
  height: 100%;        /* the height of its list item */
}

片段:

.sub-menu li {
  position: relative;  /* to contain the absolute-positioned pseudo-element */
}

.sub-menu a:before {
  content: '';         /* required for most pseudo-elements */
  position: absolute;  /* absolutely positioned */
  top: 0;              /* ... at top of its list item */
  left: -50px;         /* ... to the left of its list item, including the bullet */
  right: 0;            /* to its list item's right */
  height: 100%;        /* the height of its list item */
}
<ul class='sub-menu'>
  <li><a href='something'>something</a></li>
  <li><a href='else'>else</a></li>
  <li><a href='another'>another</a></li>
</ul>


0
给 ul 元素添加 font-size: 0;,然后在列表项或锚点元素上设置字体大小。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接