简体   繁体   English

在CSS中组合伪选择器?

[英]Combining Pseudo-selectors in CSS?

I mean if I need, for example, selected text in a hovered link be red, could I use the following code in CSS style? 我的意思是,如果我需要,例如,悬停链接中的选定文本是红色的,我可以在CSS样式中使用以下代码吗?

.abc:hover:selection{color:red}

and

<a href="123" class="abc">4567890</a>

Would that link, when I select part of it, become red colored when I hover it and is this correct syntax for such pseudo-classes combining? 当我选择它的一部分时,那个链接会在我悬停它时变成红色,并且这种伪类合并的正确语法是什么?

If you're talking about pseudo-classes, then yes, you can combine them in any order. 如果您正在谈论伪类,那么是的,您可以按任何顺序组合它们。

Except in this case, ::selection is not a pseudo-class, it's a pseudo-element that's not part of CSS1 or CSS2, or any current spec for that matter. 除了在这种情况下, ::selection不是伪类,它是一个伪元素,它不是CSS1或CSS2的一部分,或任何当前的规范。 And this is where the term "pseudo-selector" falls short, because they're two completely different things. 这就是术语“伪选择器”不足之处,因为它们是两个完全不同的东西。

The correct syntax is a single colon for :hover and double colons for ::selection , and unlike pseudo-classes, pseudo-elements must always come last: 正确的语法是单个冒号:hover用于::selection :hover和double冒号,与伪类不同,伪元素必须始终排在最后:

.abc:hover::selection{color:red}

And even then, because of the way ::selection works (or doesn't), it's not guaranteed to actually have an effect in browsers. 即便如此,由于::selection的方式有效(或没有),因此无法保证在浏览器中实际产生效果。

 .container:nth-last-child(2):not(:first-child) {
    background-color: red;
 }

Yes, it is the recommended way to do it, see the w3c about pseudo classes : http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes 是的,这是推荐的方法,请参阅关于伪类的w3c: http//www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes

EDIT : as BoltClock pointed out, this link if for CSS2, this is the document for CSS3 : http://www.w3.org/TR/css3-selectors/#the-user-action-pseudo-classes-hover-act 编辑:正如BoltClock所指出的,这个链接如果是CSS2,这是CSS3的文档: http//www.w3.org/TR/css3-selectors/#the-user-action-pseudo-classes-hover-act

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM