17

If I need selected text in a hovered link be red, could I use the following code in CSS style?

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

5
  • @David Thomas: I should know is it correct way or not
    – el Dude
    Commented Jul 3, 2013 at 18:46
  • When in doubt, validate. If it doesn't validate, then it's clearly wrong.
    – cimmanon
    Commented Jul 3, 2013 at 19:00
  • @cimmanon: it's much simple to ask =)
    – el Dude
    Commented Jul 3, 2013 at 19:26
  • It's simpler to ask SO than to validate? Makes no sense to me.
    – cimmanon
    Commented Jul 3, 2013 at 20:18
  • 3
    This is one of those rare times I have to downvote the question because when prompted to test the asker outright refuses to do so, and yet I have to post my own answer anyway and downvote the existing answer because it is wrong.
    – BoltClock
    Commented Jul 4, 2013 at 17:17

2 Answers 2

27

Yes, you can combine pseudo-classes 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.

1
  • But it could be combined amd it is correct way. Thats what I was looking for
    – el Dude
    Commented Jul 4, 2013 at 21:08
2
 .container:nth-last-child(2):not(:first-child) {
    background-color: red;
 }
1
  • 10
    Please provide some explanation - code only answers are not necessarily helpful. How does this answer the question?
    – MrWhite
    Commented Dec 10, 2018 at 8:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.