Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate the CSS transition property within :after/:before pseudo-classes

Is it possible to animate CSS pseudo-classes?

Say I have:

#foo:after {     content: '';     width: 200px;     height: 200px;     background: red;     display: block;     -webkit-transition: all 1s ease-in-out;     -moz-transition: all 1s ease-in-out;     -o-transition: all 1s ease-in-out; }  #foo:hover:after {     width: 250px;     height: 250px; } 

Is this even possible? I've been testing and so far I can't seem to find a solution. I'm trying to trim down the amount of JavaScript support I need by using Modernizr.

I already have a JavaScript method, so please no JavaScript alternatives.

Demo: http://jsfiddle.net/MxTvw/

like image 927
daryl Avatar asked Oct 18 '11 15:10

daryl


People also ask

Can you animate pseudo-elements CSS?

Pseudo-elements are like getting extra DOM elements for free. They allow us to add extra content, decoration and more to our pages without adding extra HTML, and they can be animated. In this post we'll use a pseudo-element to add a little visual flair to a button.

What is before and after pseudo selectors in CSS?

Definition and Usage The ::before selector inserts something before the content of each selected element(s). Use the content property to specify the content to insert. Use the ::after selector to insert something after the content.

What property do you use in conjunction with the before pseudo-element to display content before an element?

The content CSS property is used in conjunction with these pseudo-elements, to insert the generated content.

Which CSS property is used for transition effect?

The transition-property property specifies the name of the CSS property the transition effect is for (the transition effect will start when the specified CSS property changes). Tip: A transition effect could typically occur when a user hover over an element.


1 Answers

Your fiddle does work for me in Firefox. And as far as I know, and if this article is up to date this is the only browser that can animate pseudo-elements.


EDIT: As of 2016, the link to article is broken and the relevant WebKit bug was fixed 4 years ago. Read on to see other answers, this one is obsolete.

like image 64
Litek Avatar answered Oct 14 '22 17:10

Litek