html - 如何删除链接 <a> 和它位于 <li> 中的列表项之间的所有填充?或者...点击 <li> 打开里面的链接?

标签 html css hyperlink padding listitem

我的代码:

<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 以尝试强制执行我想要的行为,但仍然没有成功。帮忙?

最佳答案

在 anchor 上创建一个伪元素,覆盖它们的列表项,包括元素符号:

.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>

关于html - 如何删除链接 <a> 和它位于 <li> 中的列表项之间的所有填充?或者...点击 <li> 打开里面的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48450130/

相关文章:

html - 加密的YouTube网址无法正常工作,但可以使用 '='

javascript - 使 Knockout applyBindings 将选择选项视为数字

jquery - 将类添加到自定义 jQuery Accordion

html - 如何在页面上水平对齐 bootstrap div?

javascript - 检测输入中输入的单词和字母,并对具有相同字母的其他元素(div 中的文本)进行排序

hudson - Redmine 插件在 hudson 中不起作用

html - GWT : Sound not playing mobile?

rest - 应该对 json-api 响应中的链接进行编码吗?

html - 为什么在我的 <a> 元素下会出现 4px 的额外填充?

javascript - 如何编写一个 JS 按钮来激活单击和滚动时的转换?