Yes, it is possible.
.boo {
position: relative;
width: 20em; min-height: 10em;
background: rgba(0,0,0,0) url(http://placekitten.com/320/160);
transition: background-color 1s;
}
.boo:hover {
background-color: rgba(0,0,0,.5);
}
.boo:before {
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
background-color: inherit;
content: ' ';
}
What am I doing here?
What I am doing here is that I am setting a RGBa background-color
on the div
, behind its background-image
and transitioning this background-color
(its alpha) on :hover
. All this happens behind the background-image
. However, I am also using background-color: inherit
on the pseudo-element, which means that, at any given moment, the pseudo-element, which is situated above its parent div
(and therefore above the background-image
of the div
) is going to have the same background-color
(meaning that the background-color
of the pseudo-element is going to transition from rgba(0,0,0,0)
to rgba(0,0,0,.5)
on :hover
).
Why do it this way?
The reason why I am not transitioning directly the background-color
of the pseudo-element is that support for transitions on pseudo-elements is still not that good yet.
Support for transitions on pseudo-elements
✓ Firefox supports transitions on pseudo-elements and has supported them for quite a while, let's get this out of the way first.
✗ Current versions of Safari and Opera don't support transitions on pseudo-elements.
Chrome supports transitions on pseudo-elements only starting from version 26.
IE10 supports them in a bit of a weird way, meaning that something like:
.boo:before { color: blue; transition: 1s; }
.boo:hover:before { color: red; }
won't work, you have to specify the hover state on the element itself as well. Like this:
.boo:hover {}
.boo:before { color: blue; transition: 1s; }
.boo:hover:before { color: red; }
More info and examples about how you can transition various properties of pseudo-elements using this inherit
technique: http://vimeo.com/51897358
EDIT
Transitions directly on pseudo-elements are now supported in Opera since the switch to Blink and in Safari since 6.1.