Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Transitions and Transforms Getting Started with CSS Transitions Creating Your First Transition with transition-duration

Elias Paiva
Elias Paiva
7,348 Points

Why was the declaration "transition-duration: 1s;" added to ".button" and not ".button:hover"?

I thought that it was going to be added to the hover declariton, but I tested it and it works both ways. So why one over the other)

2 Answers

Hi Eias!

I think that's just the customary way to approach it.

Think of button as the starting point and button: hover as the endpoint

My way of looking at it is you include the transition/duration on the starting point.

Does that make sense?

BTW, I tested it too, and it does SEEM to work either way.

NOTE: But there is a subtle difference (which I didn't notice either at first):

When transition-duration is assigned to button, the duration works BOTH when you hover on and hover off.

When it is assigned to button:hover, however you get a smooth, slow transition when you hover on only.

When you hover off, it abruptly reverts back (without the smooth, slow transition).

(Test it again and you'll see it.)

So, I learned something new, too!?! LOL (Thank you for that!)

More info:

https://www.w3schools.com/css/css3_transitions.asp

https://css-tricks.com/almanac/properties/t/transition/

I hope that helps.

Stay safe and happy coding!

Soleil Walker
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Soleil Walker
UX Design Techdegree Graduate 14,677 Points

To add to Peter's response, I've asked ChatGPT about this. In summary, it says that it is done this way for keeping your code consistent, clear, efficient, and easy to maintain.

To expound on these, by defining the property in the original state rule:

  1. Consistent - We're ensuring that the transition behaves consistently across the various different pseudo states an elements might be styled to have. Also for example, not just when hovering on, but also hovering off.

  2. Clear - We're separating behavioural declarations from styling declarations. The default behavioural declarations are added to the original state rule and, the unique styling declarations are added to each individual pseudo-class state.

  3. Efficient - We're preventing the browser from having to parse the code and process that default rule every time the state changes.

  4. Ease of Maintenance - We're making our code easier to maintain by having it in one place rather than multiple places in case we need to update it.