A Bit About CSS Transition Property

If you are bored because you have to play with text and color in web design, let me introduce you to unique, bombshell, explosion-worthy property. OK, maybe not so much explosive but you are going to enjoy it. CSS follows and supports your willing to spice things up and that is why there is a transition option.

About Transition

The transition property allows changing the CSS property values smoothly.
It is necessary to define a property that will be changed and the duration of the effect.

button{
transition: background 1s;
-webkit-transition: background 1s; /* Safari */
}

These two values can be defined with the universal property transition or with individual properties transition-property and transition-duration.

Some of the transition-property values I can remember at the moment are:

  • width,
  • height,
  • color,
  • background (color, image, position) ,
  • transform (in the next post),
  • border (color, width),
  • position (top, bottom, left, right ),
  • text (size, weight, shadow, word-spacing),
  • margin,
  • padding,
  • opacity,
  • visibility,
  • z-index,
  • all.

If the value is defined with a property, defines a comma-separated list of CSS property names the transition effect is for. The value all is the default one and defines that all properties that can transition will transition. In this case, all changed properties will have a transition with the same duration (and delay and timing if defined).

The transition-duration property is very simple, it defines how much time a transition effect takes to complete, and its value can be defined in seconds (s) or milliseconds (ms).

More Transition Options

Additional properties connected to transition are:

  • transition-delay
  • transition-timing-function

Very similar to the duration property, transition-delay is defined with seconds (s) or milliseconds (ms) and specifies when the transition effect will start. This property can be negative, unlike the duration property, and if that so it will begin part-way through its play cycle.

The transition-timing-function property is defined by the following functions:

  • ease — slow start, fast middle, slow end,
  • linear — constant speed,
  • ease-in — slow start, fast end,
  • ease-out — fast start, slow end,
  • ease-in-out — more pronounced acceleration/deceleration curves that ease,
  • cubic-bezier (n,n,n,n) — you can write your own function defined with 4 coordinates (cubic bezier functions).
button{
transition: background 1s ease-in-out 2s;
-webkit-transition: background 1s ease-in-out 2s; /* Safari */
}

With the universal property transition you can define all four properties, just keep in mind that first time defined is always transition-duration.

It’s possible to make the multiple transitions set. To split different transitions use ,.

button{
transition: background 1s ease-in-out 2s, width 2s linear;
-webkit-transition: background 1s ease-in-out 2s, width 2s linear; /* Safari */
}

Triggering

The main thing to remember about the transition is that this property is defined before and it’s going to happen only if it’s triggered (if the property which is defined with transition-property is changed).

You can trigger CSS transitions directly with pseudo-classes like :hover, :focus and :active or through JavaScript by adding or removing classes.

Final Word

This property is widely supported so use it! If you need some inspiration, check out this example.

Originally published at kolosek.com on April 10, 2018.

More by Alexandra Mladenovic

Topics of interest

More Related Stories