How to use CSS transitions

CSS transitions provide a way to control animation speed when changing CSS properties. Instead of having property changes take effect immediately, you can cause the changes in a property to take place over a period of time.

Multiple animated properties example

.box {
border-style: solid;
border-width: 1px;
display: block;
width: 100px;
height: 100px;
background-color: #0000EE;
-webkit-transition: width 2s, height 2s, background-color 2s, -webkit-transform 2s;
transition: width 2s, height 2s, background-color 2s, transform 2s;
}

.box:hover {
background-color: #FF0000;
width: 200px;
height: 200px;
-webkit-transform: rotate(180deg);
transform: rotate(180deg);
}

CSS properties used to define transitions

CSS Transitions are controlled using the shorthand transition property. You can control the individual components of the transition with the following sub-properties:

transition-property

Specifies the name or names of the CSS properties to which transitions should be applied.

/* Keyword values */
transition-property: none;
transition-property: all;
transition-property: test_05;
transition-property: -specific;
transition-property: sliding-vertically;

transition-property: test1;
transition-property: test1, animation4;
transition-property: all, height, all;
transition-property: all, -moz-specific, sliding;

/* Global values */
transition-property: inherit;
transition-property: initial;
transition-property: unset;

transition-duration

Specifies the duration over which transitions should occur. You can specify a single duration that applies to all properties during the transition, or multiple values to allow each property to transition over a different period of time.

/*

/* Global values */
transition-duration: inherit;
transition-duration: initial;
transition-duration: unset;

transition-delay

Defines how long to wait between the time a property is changed and the transition actually begins.

/*

/* Global values */
transition-delay: inherit;
transition-delay: initial;
transition-delay: unset;

This example performs a four-second font size transition with a two-second delay between the time the user mouses over the element and the beginning of the animation effect:

#delay1 {
position: relative;
transition-property: font-size;
transition-duration: 4s;
transition-delay: 2s;
font-size: 14px;
}

#delay1:hover {
transition-property: font-size;
transition-duration: 4s;
transition-delay: 2s;
font-size: 36px;
}

Using transitions when highlighting menus

html

<div class=”sidebar”>
<p><a class=”menuButton” href=”home”>Home</a></p>
<p><a class=”menuButton” href=”about”>About</a></p>
<p><a class=”menuButton” href=”contact”>Contact Us</a></p>
<p><a class=”menuButton” href=”links”>Links</a></p>
</div>

CSS

.menuButton {
position: relative;
transition-property: background-color, color;
transition-duration: 1s;
transition-timing-function: ease-out;
text-align: left;
background-color: grey;
left: 5px;
top: 5px;
height: 26px;
color: white;
border-color: black;
font-family: sans-serif;
font-size: 20px;
text-decoration: none;
box-shadow: 2px 2px 1px black;
padding: 2px 4px;
border: solid 1px black;
}

.menuButton:hover {
position: relative;
transition-property: background-color, color;
transition-duration: 1s;
transition-timing-function: ease-out;
background-color:white;
color:black;
box-shadow: 2px 2px 1px black;
}

transition-timing-function

You may specify multiple timing functions; each one will be applied to the corresponding property as specified by the transition-property property, which acts as a master list. If there are fewer functions specified than in the master list, missing values are set to the initial value (ease). If there are more timing functions, the list is simply truncated to the right size. In both case the CSS declaration stays valid.

The transition-timing-function property can have the following values:

  • ease – specifies a transition effect with a slow start, then fast, then end slowly (this is default)
  • <code “>linear – specifies a transition effect with the same speed from start to end
  • ease-in – specifies a transition effect with a slow start
  • ease-out – specifies a transition effect with a slow end
  • ease-in-out – specifies a transition effect with a slow start and end
  • cubic-bezier(n,n,n,n) – lets you define your own values in a cubic-bezier function



/* Keyword values */
transition-timing-function: ease;
transition-timing-function: ease-in;
transition-timing-function: ease-out;
transition-timing-function: ease-in-out;
transition-timing-function: linear;
transition-timing-function: step-start;
transition-timing-function: step-end;

/* Function values */
transition-timing-function: steps(4, end);
transition-timing-function: cubic-bezier(0.1, 0.7, 1.0, 0.1);

/* Multiple timing functions */
transition-timing-function: ease, step-start, cubic-bezier(0.1, 0.7, 1.0, 0.1);

/* Global values */
transition-timing-function: inherit;
transition-timing-function: initial;
transition-timing-function: unset;

Leave a Reply

Your email address will not be published.

*