Combining Pseudo-selectors in CSS?

前端 未结 2 661
一个人的身影
一个人的身影 2021-01-17 18:03

I mean if I need, for example, selected text in a hovered link be red, could I use the following code in CSS style?

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


        
相关标签:
2条回答
  • 2021-01-17 18:34
     .container:nth-last-child(2):not(:first-child) {
        background-color: red;
     }
    
    0 讨论(0)
  • 2021-01-17 18:40

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

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

    0 讨论(0)
提交回复
热议问题