Maintaining CSS Style States using “Infinite” Transition Delays

March 19th, 2012 :: Web Development

Earlier today I discovered an interesting way to keep (store) a CSS style on an element using CSS transitions. This is probably best explained with an example, so here goes:

an hourglass

This text will only have the color while the button is pressed down.

This text will keep the given color even after the button is released.

As far as CSS only solutions go, there are two other tricks that can be used to achieve this similar behavior: using either the :checked or the :target pseudo selectors. In this post, I’ll show you my CSS transition technique used above, followed by a slightly cooler example that I’ve been working on.


<button class="red">Red</button>
<button class="green">Green</button>
<button class="blue">Blue</button>

This text will only have the color while the button is pressed down.
<p class="perm">
This text will keep the given color even after the button is released.


The prefixes below have been omitted for brevity.

/* The large delay prevents the color from changing */
p.perm {
	transition: color 0s 9999999s;

/* When a button is pressed, overwrite the transition property
   to remove the delay, so that we can instantly change the color */
.red:active ~ p.perm, .green:active ~ p.perm, .blue:active ~ p.perm {
	transition: color 0s; 
.red:active ~ p {
	color: red;
.green:active ~ p { 
	color: green; 
.blue:active ~ p {
	color: blue;

The JavaScript

// Absolutely none

How It Works

It’s actually pretty simple. With our first CSS declaration, we’ve made it so that any changes to the color property will be delayed by approximately 116 days ( :D ). This virtually infinite delay makes sure that the color stays the same once we’ve set it.

The trick now is to somehow temporary get around this delay, so we can apply the different colors with the buttons. This is done by overriding the transition property during the button press, setting the delay to 0. Now when we release the button, the old transition property will kick back in, setting the delay to 116 days. This will make sure that the text will keep the new color instead of going back to the default.

As far as I know, this only works with animatable properties, but a cool fact is that you can end a transition halfway through and have the property still keep the exact value that it had at that point in the transition. So for example, let’s say we have a linear transition from opacity 1 to 0 over two seconds, but only let it run for one second. The opacity value should now be stuck at 0.5 because the delay prevents it from going back. If we now were to run the opacity transition again, it would continue from 0.5 instead of starting over from the beginning.

Now, before you run off and start using this technique all over the web, keep in mind that this is a very dirty hack for achieving something with CSS that ideally should be done with JavaScript.

Taking it a step further

As I mentioned, I’ve been working on something cool to show this off, so make sure to check out the demo below.


  • loktar

    Very cool! Only thing I noticed as a bug is if I click on one direction multiple times the characters animation and movement speed decrease for that direction, until you travel in a different direction then the speed is reset to normal.

    • JoelBesada

      Yeah, the closer you are from an edge when you start walking, the slower the character will move. This is because it always transitions toward the edge of the direction over 2 seconds, so if you’re already close to the destination you’ll move much slower.

    • Lea Verou

      You can solve that by using transition-timing-function: linear; 

    • JoelBesada

      I already am, that’s not the core problem. Let me see if I can explain this a bit clearer.

      Whenever you hold down one of the directional buttons, a 2 second transition starts to move the character towards the correlating wall. This transition will *always* be 2 seconds, regardless of how far the character has to move. 

      Let’s say you start moving the character towards a wall that is 200 pixels away. This means you’ll move at a speed of 100 pixels per second. Now, let’s say that you release the button after one second, and then press it down again. The remaining distance, 100 pixels, will now also be transitioned over 2 second duration, which means the character now moves at a speed of 50 pixels per second. 

      I don’t think there is any solution to this as far as CSS goes.

    • Lea Verou

      Oohh, I get it now. Yeah, can’t think of a solution either. I’ve often wished transitions allowed speeds not just durations.

    • Alan_F

       Redraw the background as water that gets deeper towards the edges – problem solved.

    • John Pencola

      Good idea… except for when you reverse direction while in the water ;)

  • Tony Hue

    Neat trick! 

  • Krupinski Rafal

    If you fall asleep during play, lets say for 99999s, you can wake up with al your progress lost :D

    • Dina George

      Yeah, this seems legit :)

  • Prisca Schmarsow

    very clever ;) love it ~ thanks for sharing ;)

  • Michel Plungjan

    Is this supposed to work in Fx12 on OSX? I do not get the persistence

  • Matt Curtis

    Me and Mike are in the same boat – not working in my version of FF either (which would be 11.0)

  • Richard – accessibleweb

    Not working in Firefox

  • Pingback: Design | Pearltrees

  • nicolas

    neat idea! works here (firefox 14).

  • JoelBesada

    Oh right, I forgot that Firefox needs the “s” for the time values in CSS transitions, even when it’s set to 0. Should work now.

  • cballenar

    That’s pretty neat! I’m wondering if having multiple elements doing this could slow down the browser. It must be creating some kind of timer right?
    Thanks for sharing, I love to see CSS being used in new, devious ways :)

    • JoelBesada

      Just having one or a couple of delay timers ticking in the background shouldn’t slow down the browser, but I haven’t done any research on this so I’m not 100% sure. This might also vary a lot between browsers.

  • Pingback: Maintaining CSS Style States using “Infinite” Transition Delays | Qtiva

  • Pingback: Maintaining CSS Style States using “Infinite” Transition Delays

  • Joshua Hibbert

    This is fantastic Joel, awesome discovery!

    I was recently playing around with a CSS ‘hover to check’ idea that I had, and I have been able to incorporate your concept:

    • JoelBesada


      I’m excited to see more things that this can be used for, keep ‘em coming! 

  • wo_is神仙


  • Pingback: My Stream | Maintaining CSS Style States using “Infinite” Transition Delays | My Stream

  • Pingback: Maintaining CSS Style States using “Infinite” Transition Delays - CSS Feed

  • Karl

    Heya, very cool tip.
    I have another question though, whats upp with the use of this:  ~ Haven’t seen this used in CSS before and would be glad to get a bit of input where to deepen on this. Does it actually do anything? Could I write the same with just a singe space between declarations? Couldn’t find any doc on use of that char ( ~ ).

    Thanks a million guys!

  • Pingback: Maintaining CSS Style States using “Infinite” Transition Delays | BorderCut

  • RaaZ Shrestha

    wow. its cool. 

  • Pingback: Maintaining CSS Style States using “Infinite” Transition Delays | DesignPostBookmarkThing

  • Pingback: Maintaining CSS Style States using “Infinite” Transition Delays | hey you design blog

  • Pingback: Maintaining CSS Style States using “Infinite” Transition Delays | XF Themes

  • Pingback: Maintaining CSS Style States using “Infinite” Transition Delays |

  • Rob Wierzbowski

    I was expecting an excruciatingly complex mess of css rules on your Dabblet example, but it’s sooo simple. Amazingly so. 

    Hats off, very cool.

  • Rafał Krupiński

    For some usecases this would do:

    • JoelBesada

      Oh, nice!
      There’s probably a lot of cool tricks and hacks using CSS animations as well, would be interesting to experiment around with these properties to see what can be done.

  • Pingback: HTML & CSS From Around the Web on March 23, 2012 –

  • Pingback: Friday Focus 03/23/12: Toned Down | Devlounge

  • Pingback: Collective #4 | Wordpress Themes

  • Pingback: Collective #4 « Blog de xkortazar

  • Zim

    Interesting idea :)

  • Adam Stanley

    Joe – great demo!  In your demo on dabblet, you mentioned “
    please keep in mind that things like this should ideally be handled with Javascript.” but I disagree!  With CSS you can get native hardware acceleration from some browsers (not the case w JavaScript).

    I don’t think this is a hack at all :)  Totally legit way of building awesome web content!

    Have you seen this? 

    • Patchsofts

      is this is

  • Guillaume Esquevin

    You could use
    animation-fill-mode: forward;

    To stay on your ending state

  • Ivanov

    it does not work on Opera 11.62

  • gentlemedia

    I like this very much… played a bit with it because I wanted to see if this also works with the hover pseudo class, but unfortunately this doesn’t work. Why does it work on :active and not on :hover?

  • gentlemedia

    Forget my comment… you can delete it and this one too if you want… my target element  was not a sibling from the trigger element. All good now :)

    • JoelBesada

      I’m glad you figured it out :)

  • Sandeep Virk

    Hi joel,

    can you explain why CHAR stick to 224px dimension because when i increase the width of .screen DIV but CHAR still stop at 224px width.

  •ชนม์บวร-โกมุทบพิตร/100002564835362 ชนม์บวร โกมุทบพิตร

    What about if user visiting more 9999999s ?

  • Roto2store

    great tip, thank u!

  • ecommerce review

    Nice post! I know t

  • Interested

    Nice concept. I like it a lot. Though if you move to any direction several times it tends to get slower and slower until you move some other direction :)

  • Zihaib Hashmi

    It’s actually a nice and useful piece of information.

    I’m happy that you simply shared this useful information with us. ,, my name is naveed

  • Zihaib Hashmi

    Usually I don’t learn post on blogs, but I would like to say that this write-up very compelled me to
    my name is naveed

  • farjad taher

    so great and nice….driver toolkit 8.4 license key

  • mariakhan

    I agree with you @zabi74hashmi it really is amazing and also inspiring i shared it to may friends. Zcracksdotcom is here Inshasoftdotcom Cracknewdotnet

  • Sania Batool

    i enjoyed the review,. it is well written and almost cover whole plot of the book. If anyone wants to download softwares like Dll Files fixer crack included You will love our blog….

  • Hina Ismail

    I have been a nerd from the start for CSS editing…I even can not edit css stylesheet for my site..Look at this page saying Mixcraft 6 Registration code

  • wordpress themes

    really awesome info for me
    jingling download

  • Jack Dowson

    i like your web site.

    Cursor Fx Plus 2.11 Full crack

  • Danish Daniel

    i really like your post and content is amazed write also yor site is going nyc thanks admin..AVS Video Editor 7.1 Crack

  • Jack Dowson

    Pretty! This was a really wonderful post. thanks for providing
    this info.

    DRIVER Toolkit 8.4 crack

  • atifa

    hello admin. I visit your website. you have good works .keep it up.

    Hotspot shield 4.20.0 Elite crack

  • Windows 8.1 Activator Loader

    amazing post keep it up:)

    Facebook Hacker 3.6

  • danish

    Your site posting is very good i realy appriciated for your posting..Norton Antivirus 2016 Activation Key

  • Hoorain malik

    fantastic post keep it up.

    PhotoShine 6.3 Crack

  • Crack Doner

    Nice Work For maintaining CSS Styles It works Really Awsome Thanks Friend For The Help From: Crack Doner

  • Crack Doner

    Crack Doner
    Having Great Full to Visit THis Informative Blogg

  • Britin Alden

    The way you described the layout of CSS Style is amazing!
    SpyHunter 4 Email Password

    • Aydan

      yeah that is thank you

  • Haseeb Ur Rehman

    the description is more helpful for learner thanks

  • Hina Khan

    How you described design of CSS Style is amazing! SoftwarezTab Rana PC Saira PC Beta Cracks Chosni.Com

  • Noor Hayat

    WordPress Plugin Woocommerce
    Its really helpful article , i read it i just like your effort this shows how much you have interest in these type of articles i hope you will keep writing for us i like your effort.Thank you soo much for sharing an awesome information

  • Olivia

    wao excellent and so amazing blog. The way you expressed and layout of CSS style is very great. thanks for sharing us important information.
    Windows 10 crack

  • Hina Khan

    Useful Post Thanx for Share you can also check my blog Rana PC Games

  • Jon Joni

    I had been expecting an excruciatingly complex mess of css rules in your Dabblet example, but it is sooo simple. Amazingly so us jobs data

  • Anum balouch
  • Isabella

    how you described design of CSS Style is amazing! Serial Key Pro

  • amanda

    are you able to explain why CHAR stay with 224px dimension because after i boost the width of .screen DIV but CHAR still visit 224px width. Shafaqat Pc

  • jaam
  • Haseeb Khan

    you did awesome job. i exactly found what i want CSS style i like your site most best i ever seen others. thanks for this sharing keep it up thanks alot.
    VirtualDj Crack

  • Anmol Shahzadi

    wow. useful and nice sharing.thanks a lot ADMIN
    wps office crack

  • Imaan Shah

    Aw, this was an incredibly nice post. Finding the
    time and actual effort to create a very good article… but what can I say… I procrastinate a lot
    and don’t manage to get anything done. Tally ERP 9 Crack Greetings, just wanted to leave a post on the site to share the great experience that was had with the above editorial. bitdefender total security 2018 license key There was fantastic enquiries off the back of this. Imaginative customer service, great plan and would wholeheartedly work with Softech again. Little Snitch 4.0.3 Crack
    I was excited to find this site. I need to to thank you for your time for this fantastic read!! I definitely loved every part of it and I have you bookmarked to look at new things in your blog. Sylenth1 3.032 Crack

  • Cracked softwares

    This is very interesting, You’re a very skilled blogger. I have joined your feed and look forward to more of your magnificent post. Also, I’ve shared your website in my social networks! FL Studio
    Tenorshare ReiBoot Pro Crack

  • farjad tahir

    I tried this it really works thanks admin for sharing this VirtualDj Pro

  • MR martin

    wow. useful and nice sharing.thanks a lot ADMIN
    iTools Keygen

  • mr shahed

    Amazing post. thanks
    Rhinoceros Crack

  • Imaan Shah

    It’s actually very complicated in this busy life to listen to news on Television, thus I
    simply use internet for that reason, and obtain the most recent information. PhpStorm 2017.3

  • Imaan Shah

    GREAT WORK AGAIN 4k Video Downloader Key

  • Engr Shafqat Badar

    informative style for CSS candidates. thaks for sharing.
    Little Snitch Serial Number Avira Antivirus Pro 2018 Crack