17

How do I use a SASS selector within a pseudo selector on a hover state? Wanting to write less code and clean this up.

Preferred method, not working:

div {
    a {
        transition: all 0.25s ease-in-out;
        &:after {
            content: ">>";
            left: 10px;
            :hover & {
                left: 10px;
            }
        }
    }
}

As opposed to:

div {
    a {
        transition: all 0.25s ease-in-out;
        &:after {
            content: ">>";
            left: 10px;
        }
        &:hover {
            &:after {
                left: 10px;
            }
        }
    }
}
Alexander Graham
  • 407
  • 1
  • 3
  • 10
  • `:hover &` targets `:hover` *inside* of the `:after`, rather than on it, does it not? – TylerH Apr 29 '16 at 19:37
  • On the first example it does. Trying to grab the outside parent element as a selector. – Alexander Graham Apr 29 '16 at 19:38
  • Did you even *look* at the compiled output? When there's only one possible order of selectors that will work, you have to write the selectors in that order. No amount of wishing will change anything. – cimmanon Apr 29 '16 at 20:06

1 Answers1

26

Use &:hover:after {} for targeting the after pseudo-element of the element being hovered over.

&:after:hover {} would select the after pseudo-element and used on hover it.

General_Twyckenham
  • 2,161
  • 2
  • 21
  • 36
Jan Franta
  • 1,691
  • 2
  • 16
  • 25