Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS (transition) after a pseudo element - how to transition content that shows on hover

People also ask

Does transition work only on hover?

External JavaScript Transitions allow you to alter the behavior and appearance of an element — but only when there's a trigger, like a user hovering over the element. Once triggered, transitions can only move from an initial state to a final state.

How do you transition after CSS?

after is not a valid value of transition . Instead put transition as a property of the :after selector. You can also have a different transition on the hover in and hover out state. This allows us to have a delay to show the pseudo-element but no delay to hide it.

What is hover transition?

CSS transitions allows you to change property values smoothly (from one value to another), over a given duration.


after is not a valid value of transition.

Instead put transition as a property of the :after selector.

HTML

<div>Test</div>

CSS

div:after {
    content:" - positive!";
    position: relative;
    opacity: 0;
    top: -20px;
    -webkit-transition: all 3s;
    transition: all 3s;
}
div:hover:after {
    opacity: 1;
    top: 0px;
}

Demo

You can also have a different transition on the hover in and hover out state. This allows us to have a delay to show the pseudo-element but no delay to hide it.

CSS

div:after {
    content:" - positive!";
    position: relative;
    opacity: 0;
    top: -20px;
    -webkit-transition: all 250ms;
    transition: all 250ms;
}
div:hover:after {
    opacity: 1;
    top: 0px;
    -webkit-transition: all 3s;
    transition: all 3s;
}

Demo

Here is a list of browsers that support transitions on pseudo elements: http://css-tricks.com/transitions-and-animations-on-css-generated-content/