This question seems to be the opposite of Can I target a :before or :after pseudo-element with a sibling combinator?
In your case, you cannot apply pseudo-classes to pseudo-elements. This also means you cannot choose to apply styles only when a pseudo-element is on :hover
but not its generating element. Instead, you can only select a pseudo-element depending on the pseudo-class states of its generating element, and only when this generating element is the subject of the selector.
Also, as mentioned, pseudo-elements are not part of the DOM. Therefore, similarly to how you cannot reach pseudo-elements relatively to real children within the same generating element as shown in my answer to the above question (explanation in the above link):
a[href^="http"] img ~ :after
You cannot select real elements relatively to pseudo-elements within the same generating element:
.foo:after > div
So to sum up, the reason why the following selector won't work:
.foo:after:hover > div
Is twofold:
Pseudo-elements cannot have pseudo-class states, making :after:hover
invalid.
The subject of the selector here is not .foo
, but div
, so :after
cannot apply to .foo
. Consequently, attempting to select a real element as the subject of a selector after applying a pseudo-element to another selector doesn't make sense.
If you really must not display your div
on .foo:hover
, but on another child element, then your only way around this without using JavaScript is to make another real element under .foo
that comes before the div
(not after):
<div class="foo">
<div></div>
<div>Stuff</div>
</div>
This is because there is no previous sibling selector; the adjacent sibling selector, which you can use in place of :after >
is only for selecting the next sibling:
.foo > div:first-child + div { display: none; }
.foo > div:first-child { /* rules */ }
.foo > div:first-child:hover + div { display: block; }