CSS3 Transitions: is there an on click option without using JQuery?

MeltingDog picture MeltingDog · Jun 23, 2012 · Viewed 25.4k times · Source

This works great:

<style type="text/css"> 
    div
    {
    width:100px;
    height:100px;
    background:red;
    transition:width 2s;
    -moz-transition:width 2s; /* Firefox 4 */
    -webkit-transition:width 2s; /* Safari and Chrome */
    -o-transition:width 2s; /* Opera */
    }

    div:hover
    {
    width:300px;
    }
    </style>

But does anyone know of a way to do this using click instead of hover? And not involving JQuery?

Thanks!

Answer

sandeep picture sandeep · Jun 23, 2012

You can write like this:

CSS

input{display:none}
.ani
    {
    width:100px;
    height:100px;
    background:red;
    transition:width 2s;
    -moz-transition:width 2s; /* Firefox 4 */
    -webkit-transition:width 2s; /* Safari and Chrome */
    -o-transition:width 2s; /* Opera */
    display:block;
    }
input:checked + .ani{width:300px;}

HTML

<input type="checkbox" id="button">
<label class="ani" for="button"></label>

Check this http://jsfiddle.net/nMNJE/