FAQ: Animation - Transitions - CSS Transitions

This community-built FAQ covers the “CSS Transitions” exercise from the lesson “Animation - Transitions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise CSS Transitions

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In this exercise, the transition is applied to the button class directly. I accidentally noticed it still works if you apply the transitions to the :hover pseudoclass. Does it matter which the transitions are applied to? Is there an accepted standard?

1 Like

The lesson fails to mention 2 states are required for transitions to animate except ambiguously within the stylesheet itself, using pseudo classes like, for example :hover and :active which seems like a huge omission given it’s seemingly an integral requirement to initiate animation transitions right?

Noticed that too. I think it makes more sense to put it in the hover pseudo-class. Just makes it clearer to read what’s going on for me. I’m open to hearing if it makes a difference otherwise

I also tried pasting the same code in a:hover and a declaration, and found that they both worked the same, but a simple research below proves otherwise:

Applying Transitions to the Class Directly

When you apply transitions to the class directly, the transition effects are defined for all state changes of the element. This includes not only the :hover state but also other state changes, such as when the element gains focus, is clicked, or changes due to other interactions or script manipulations.

Example:

.button {
  background-color: blue;
  transition: background-color 0.3s ease;
}

.button:hover {
  background-color: green;
}

In this example:

  • The transition property is applied to the .button class.
  • The background color transition effect will be applied whenever the background color changes, not just on hover.

Applying Transitions to the :hover Pseudoclass

When you apply transitions to the :hover pseudoclass, the transition effect is only defined for the change into and out of the :hover state.

Example:

.button {
  background-color: blue;
}

.button:hover {
  background-color: green;
  transition: background-color 0.3s ease;
}

In this example:

  • The transition property is applied to the .button:hover pseudoclass.
  • The background color transition effect will only occur when the element is hovered over and when the hover ends.