CSS中的`li:before`语句会禁用列表项中的HTML链接。

huangapple go评论86阅读模式
英文:

CSS li:before statement deactivates html links in list item

问题

我的脚本标题如下:

<!-- 开始片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-css -->
.tickList {
  margin: -0.5em 0.5em;
  list-style: none;
  list-style-position: outside;
}

.tickList li {
  padding-left: 25px;
  margin-bottom: -1.5em;
  margin-top: -1.5em;
  margin-left: -3.5em;
  text-align: left;
  position: relative;
  top: -20px;
}

.tickList li:before {
  display: block;
  content: "A5";
  font-size: x-large;
  color: #41845B;
  position: relative;
  left: -1.1em;
  top: 25px;
}

<!-- 语言: lang-html -->
<ul class="tickList">
  <li>Some text <a href="somelink">link text</a>. More text.</li>
</ul>

<!-- 结束片段 -->

现在,如果我在浏览器中查看我的页面,链接文本会被下划线并显示为链接,然而,如果我将鼠标移到上面,光标不会变化,单击它也不起作用。我进行了一些测试,结果发现,如果我从样式声明中删除 .ticklist li:before 部分(或只删除 display: block 部分),那么链接就会按预期工作。为什么这段CSS代码会破坏链接功能?

请注意,我没有包含任何外部的JavaScript包,如jQuery或其他内容。

英文:

The header of my script looks like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.tickList {
  margin: -0.5em 0.5em;
  list-style: none;
  list-style-position: outside;
}

.tickList li {
  padding-left: 25px;
  margin-bottom: -1.5em;
  margin-top: -1.5em;
  margin-left: -3.5em;
  text-align: left;
  position: relative;
  top: -20px;
}

.tickList li:before {
  display: block;
  content: &quot;A5&quot;;
  font-size: x-large;
  color: #41845B;
  position: relative;
  left: -1.1em;
  top: 25px;
}

<!-- language: lang-html -->

&lt;ul class=&quot;tickList&quot;&gt;
  &lt;li&gt;Some text &lt;a href=&quot;somelink&quot;&gt;link text&lt;/a&gt;. More text.&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

Now, if I look at my page in a browser, the link text is underlined and looks like a link, however, if I move my mouse over it, the cursor does not change and clicking on it has no effect. I did some testing and it turns out that if I remove the .ticklist li:before part from the style declarations (or just the display:block part actually), then the link works as expected. Why does this bit of CSS code break the link functionality?

Note that I am not including any outside js packages like jQuery or anything else.

答案1

得分: 1

你的 :before 元素的宽度实际上是页面的 100%,尝试定义一个固定宽度,你会发现叠加效果不再显示。

.tickList li:before {
  display: block;
  content: "A5";
  font-size: x-large;
  color: #41845B;
  position: relative;
  left: -1.1em;
  top: 25px;
  width: 50px;
}
英文:

The width of your :before element is actually 100% of the page, try defining a fixed width, and you'll see the overlay won't show anymore.

.tickList li:before {
  display: block;
  content: &quot;A5&quot;;
  font-size: x-large;
  color: #41845B;
  position: relative;
  left: -1.1em;
  top: 25px;
  width: 50px;
}

something like that

答案2

得分: 0

有多种方法可以解决这个问题,即插入的 before 元素位于列表项之上。尝试在你的 before 规则中添加 pointer-events: none;。这将阻止 before 元素成为任何指针事件的目标,实际上将事件传递到列表项:

.tickList {
  margin: -0.5em 0.5em;
  list-style: none;
  list-style-position: outside;
}

.tickList li {
  padding-left: 25px;
  margin-bottom: -1.5em;
  margin-top: -1.5em;
  margin-left: -3.5em;
  text-align: left;
  position: relative;
  top: -20px;
}

.tickList li:before {
  display: block;
  content: "A5";
  font-size: x-large;
  color: #41845B;
  position: relative;
  left: -1.1em;
  top: 25px;
  pointer-events: none;
}

或者,你还可以将 before 元素的 z-index 设置为 -1,从而将其移到页面的背后。

英文:

There are a variety of ways to fix the issue, which is the inserted before element is sitting on top of the list item. Try adding pointer-events: none; to your before rules. This will prevent the before element from being the target of any pointer events, essentially passing it down through to the list item:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.tickList {
  margin: -0.5em 0.5em;
  list-style: none;
  list-style-position: outside;
}

.tickList li {
  padding-left: 25px;
  margin-bottom: -1.5em;
  margin-top: -1.5em;
  margin-left: -3.5em;
  text-align: left;
  position: relative;
  top: -20px;
}

.tickList li:before {
  display: block;
  content: &quot;A5&quot;;
  font-size: x-large;
  color: #41845B;
  position: relative;
  left: -1.1em;
  top: 25px;
  pointer-events: none;
}

<!-- language: lang-html -->

&lt;ul class=&quot;tickList&quot;&gt;
  &lt;li&gt;Some text &lt;a href=&quot;somelink&quot;&gt;link text&lt;/a&gt;. More text.&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

Alternately you could also set the z-index of the before element to -1 which effectively moves it behind the page.

答案3

得分: 0

.tickList {
  list-style: none;
  list-style-position: outside;
  padding: 0;
  margin: 0;
}

.tickList li {
  padding-left: 1em;
  text-align: left;
}

.tickList li:before {
  content: "A5";
  font-size: x-large;
  color: #41845B;
  position: relative;
  top: .15em;
  padding-right: .2em;
}
<ul class="tickList">
  <li>一些文字 <a href="somelink">链接文字</a>。更多文字。</li>
</ul>
英文:

The same result could have been achieved with less struggle:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.tickList {
  list-style: none;
  list-style-position: outside;
  padding: 0;
  margin: 0;
}

.tickList li {
  padding-left: 1em;
  text-align: left;
}

.tickList li:before {
  content: &quot;A5&quot;;
  font-size: x-large;
  color: #41845B;
  position: relative;
  top: .15em;
  padding-right: .2em;
}

<!-- language: lang-html -->

&lt;ul class=&quot;tickList&quot;&gt;
  &lt;li&gt;Some text &lt;a href=&quot;somelink&quot;&gt;link text&lt;/a&gt;. More text.&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 01:51:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548542.html