Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine pseudo-elements with pseudo-classes?

How do I combine pseudo-elements like :after with pseudo-classes like :hover and :not?

li {
  margin-bottom: 10px;
}
li:after {
  content: '';
  display: block;
  width: 0;
  height: 3px;
  background: #009688;
  transition: width .8s;
}
li:hover:after {
  width: 100%;
}
<ul>
  <li>first</li>
  <li>second</li>
  <li>third</li>
  <li>forth</li>
  <li>fifth</li>
</ul>

How can I exclude, for example, the first and third items in the list from this hover effect?

like image 458
kero Avatar asked Sep 14 '16 19:09

kero


People also ask

Can you combine pseudo-classes?

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.

How pseudo-element and pseudo-class are related?

Pseudo-classes enable you to target an element when it's in a particular state, as if you had added a class for that state to the DOM. Pseudo-elements act as if you had added a whole new element to the DOM, and enable you to style that.

How do you add multiple pseudo-classes in CSS?

:last-child is a pseudo-class, whereas :after (or ::after in CSS3) is a pseudo-element. To quote the standard: Pseudo-classes are allowed anywhere in selectors while pseudo-elements may only be appended after the last simple selector of the selector. This means your syntax is correct according to CSS2.

Can you use multiple pseudo-class selectors with an element?

Adding multiple pseudo elementsYou can combine several CSS pseudo-elements for one element. However, you cannot use CSS ::after two times or ::before two times. In the example below, the first letter of <div> is green and has x-large font size.


1 Answers

There are no special rules around combining pseudo-classes and pseudo-elements, besides the one rule that says there can only be one pseudo-element per complex selector and it must appear at the very end. Pseudo-classes can be written in any order — the order of simple selectors doesn't change the meaning of a compound selector. Note that a pseudo-element, unlike a pseudo-class, is not a simple selector.

You can write any of

li:not(:nth-child(1)):not(:nth-child(3)):hover:after

or

li:hover:not(:nth-child(1)):not(:nth-child(3)):after

or

li:hover:not(:nth-child(3)):not(:nth-child(1)):after

or, heck, even

li:not(:nth-child(3)):hover:not(:nth-child(1)):after

and you would get the same result (assuming the browser isn't buggy), as long as :after, the pseudo-element, appears last (and li, the type selector, appears first).

By convention, most authors choose to place structural pseudo-classes, such as :nth-child(), before dynamic pseudo-classes, such as :hover. But this is entirely personal preference; it doesn't matter to the browser.

like image 193
BoltClock Avatar answered Oct 05 '22 10:10

BoltClock