"Introduction to HTML and CSS (2016)" was retired on July 31, 2024. You are now viewing the recommended replacement.

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

Is it possible to delay a specific CSS transition?

I'm working on a project which involves a logo sliding in from out of view and changing from opacity: 0; to opacity: 1;. To do this, I'm using CSS transitions, with my styles being as follows:

#logo {
  top: -100px;
  opacity: 0;
  transition-property: opacity, top;
  transition-duration: 0.9s;
}

.animate #logo {
  opacity: 1;
  top: 40px;
  transition-delay: 0.5s;
}

The animate class is being added to the body element on page load using jQuery:

$(document).ready(function() {
  $("body").addClass("animate");
});

The issue is that the change in opacity is difficult to see, and I think this is because part of it is happening while the logo is still off-screen. Is it possible to delay the opacity change until the logo is in view?

I found the answer I needed thanks to these pages:

The latter says the following:

"You may specify multiple delays; each delay will be applied to the corresponding property as specified by the transition-property property, which acts as a master list. If there are fewer delays specified than in the master list, the list of delay values supplied will be repeated until there are enough. If there are more delays, the list is simply truncated to the right size. In both case the CSS declaration stays valid."

So, I now have the following which works:

.animate #logo {
  top: 40px;
  transition-delay: 0.8s, 0.5s;
}

1 Answer