Free Ebook cover Complete CSS course

Complete CSS course

4.5

(2)

35 pages

Transitions in CSS

Capítulo 26

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

CSS Transitions

CSS transitions allow you to create smooth and elegant animations between different states of an element. This is done by specifying the properties to animate, the duration of the animation, and the type of transition to use.

There are four main properties that can be animated in CSS:

  • background-color
  • color
  • opacity
  • transform

Each of these properties can be animated in a different way, but they all follow the same basic syntax. Here is an example of how to create a background color transition:

.button {
  background-color: #ff0000;
  transition: background-color 1s ease;
}

.button:hover {
  background-color: #00ff00;
}

In this example, the background color of the element with the button class will be animated when the mouse hovers over it. The transition property is used to specify the property that should be animated (background-color), the duration of the animation (1 second) and the type of transition to be used (ease).

You can use different transition types to create different effects. Here are some of the most common transition types:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

  • ease: starts slow, speeds up, then slows down at the end.
  • linear: keeps the same speed throughout the animation.
  • ease-in: starts slow and speeds up at the end.
  • ease-out: starts fast and slows down at the end.
  • ease-in-out: starts slow, speeds up in the middle and slows down at the end.

In addition, you can specify different durations for the animation depending on the state of the element. For example, you can create a faster transition when the mouse leaves the element:

.button {
  background-color: #ff0000;
  transition: background-color 1s ease;
}

.button:hover {
  background-color: #00ff00;
  transition-duration: 0.5s;
}

.button:active {
  background-color: #0000ff;
  transition-duration: 0.2s;
}

In this example, the background color transition has a duration of 1 second by default, but is shortened to 0.5 seconds when the mouse hovers over the element and to 0.2 seconds when the element is clicked.

CSS transitions are a great way to add a little interactivity to your designs. With a little practice, you can create smooth, elegant animations that enhance the user experience.

Now answer the exercise about the content:

_What is the basic syntax for creating a transition in CSS?

You are right! Congratulations, now go to the next page

You missed! Try again.

The basic syntax for creating a transition in CSS involves specifying the property to be animated, the duration of the animation, and the type of transition. For example, transition: background-color 1s ease; uses the property background-color, a duration of 1s, and a transition type of ease.

Next chapter

CSS Transformations

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.