我可以使用同级组合器来定位 :before 或 :after 伪元素吗?
这个 CSS 不起作用有什么原因吗?
a[href^="http"]:after {
content:"";
width:10px;
height:10px;
display:inline-block;
background-color:red;
}
a[href^="http"] img ~ :after {
display:none;
}
..在此 HTML 上?
<a href="http://google.com">Test</a>
<a href="http://google.com">
<img src="https://www.google.com/logos/classicplus.png">
</a>
这个想法是在匹配锚标记上有一个伪元素。但我不希望它应用于包裹图像的锚标记。因为我无法使用 a
a
a
a
a
a
a
a
a
a
a
a
a
a
a
a < 之类的东西来定位锚点。 img
,我想也许我可以通过查找其同级图像来定位 :after 伪元素。
任何见解将不胜感激。
Is there a reason why this CSS doesn't work?
a[href^="http"]:after {
content:"";
width:10px;
height:10px;
display:inline-block;
background-color:red;
}
a[href^="http"] img ~ :after {
display:none;
}
.. on this HTML?
<a href="http://google.com">Test</a>
<a href="http://google.com">
<img src="https://www.google.com/logos/classicplus.png">
</a>
The idea is to have a pseudo-element on matching anchor tags. But I do not want it to apply to anchor tags that wrap an image. And since I can't target anchors using something like a < img
, I figured the maybe I could target the :after pseudo-element by finding an image that it's a sibling of.
Any insight would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能以 :after 为目标,因为它的内容没有在 DOM 中渲染,也不会对其进行操作 - 要使其工作,必须重新渲染 DOM,而 CSS 无法像这样操作它。
查看规范以了解详细信息:http://www.w3.org/ TR/CSS2/generate.html#propdef-content
我建议您使用 JavaScript 来完成这项工作。
You can't target :after since it's content is not rendered in the DOM and it does not manipulate it - for this to work the DOM would have to be re-rendered and CSS can't manipulate it like this.
Check the specification for detailed understanding: http://www.w3.org/TR/CSS2/generate.html#propdef-content
I suggest you use JavaScript to do the job for you.
您不能使用组合器来定位相对于其生成元素以外的元素的伪元素。
这是因为它们是伪元素,而不是实际元素,并且组合器仅通过在实际元素之间建立关系来工作。另一方面,伪元素只能应用于选择器的主题(最右边的复合选择器),并且只有在对真实元素进行匹配处理后才会发生这种情况。换句话说,首先进行匹配,就好像伪元素不存在一样,然后将伪元素(如果在选择器中指示)应用于每个匹配项。
在您的代码中,以下选择器:
实际上并不查找位于
a
内img
之后的:after
伪元素,即使尽管看起来是这样,因为两者都呈现为a
元素的子元素。它可以重写为以下内容:
注意
*
选择器,它是隐含的。与如何在任何其他简单选择器之前省略*
以使其隐含类似,从伪元素中省略*
也会使其隐含在那里。有关详细信息,请参阅规范。现在,即使看起来
*:after
仍然应该匹配a:after
(因为a
会匹配*
) ,这样仍然不行。如果从选择器中删除:after
伪元素:您会注意到选择器的含义完全改变:
由于
img
是 HTML 中a
元素的最后一个子元素,因此没有后续同级元素可以匹配,因此没有:after
伪元素- 可以生成元素。对于
:before
或:after
伪元素,人们可能会考虑将伪元素的生成元素相对于伪元素的“同级”进行匹配,但是正如OP正确指出的那样, 没有父选择器,所以他们那里运气不好, 也。You cannot use a combinator to target a pseudo-element relative to elements other than its generating element.
This is because they're pseudo-elements, not actual elements, and combinators only work by establishing relationships between actual elements. A pseudo-element, on the other hand, can only be applied to the subject of a selector (the rightmost compound selector), and this happens only after matching is processed on the real elements. In other words, matching is done first as though the pseudo-element wasn't there, then the pseudo-element, if it's indicated within the selector, is applied to each match.
In your code, the following selector:
Does not actually look for an
:after
pseudo-element that comes after animg
within thea
, even though it appears that way as both are rendered as children of thea
element.It can be rewritten into the following:
Notice the
*
selector, which is implied. Similarly to how you can omit*
before any other simple selectors for it to be implied, omitting*
from a pseudo-element also makes it implied to be there. See the spec for details.Now, even though it appears
*:after
should still matcha:after
(sincea
would match*
), it still doesn't work that way. If you remove the:after
pseudo-element from the selector:You'll notice that the meaning of the selector changes entirely:
Since the
img
is the last child of thea
element in your HTML, there are no following siblings to match, and therefore no:after
pseudo-elements can be generated.In the case of a
:before
or:after
pseudo-element, one might think of matching the pseudo-element's generating element relative to the pseudo-element's "sibling", but as the OP has correctly pointed out, there is no parent selector, so they're out of luck there, too.