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 - Beyond the Basics Understanding CSS Transitions and Transforms Transition Timing Functions and Delays

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

what if adjacent ?

In this video the .box class will change whenever .wrap class is hovered because .box class is the child from .wrap class. Nah what if there are 2 div element with adjacent to each other called .a and .b. Then I want the condition whenever I hover the a div, the b div is transitioning without affecting the a div. so the a div just like a button to affect the b div??

<div class='b'>Transitioning when the a div is hovered</div>
<div class='a'>Stay</div>

I try this CSS but it didn't work

.b {
width: 100px;
height: 100px;
background-color: blue;
transition-duration: 1s;
}

.a {
display: block;
cursor: pointer;
width: 50px;
height:20px;
background-color: tomato; 
}
.a:hover .b {
background-color: forestgreen; 
}

Is there anyone have any idea how to do it ?? Thanks :")

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

Here, check this: http://codepen.io/kevink/pen/vLBGmJ

This works, but it's super fragile. There are a couple of rules to know. This is using the adjacent sibling selector, so first, div a has to come before div b in the dom. Your html was the other way around, in which case this won't work.

Also, no other elements can be between div a and div b in the dom. Nothing, nada. That's a rule of the adjacent sibling selector, hints it's name.

If either or these rules don't work for you, you're better off doing this via javascript.

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

Thanks kevin :") . BTW can u type in how to do it in javascript ?? Just for learning :") Thanks

Kevin Korte
Kevin Korte
28,149 Points

Sure, I used jquery, it's perfect for this kind of thing. Check it now. I disabled the adjacent selector css, and added the JS.

I added extra elements in between the two divs so you can see that in face the js version can do what the css only adjacent selector can not.

http://codepen.io/kevink/pen/GoKrQa

Let me know if that makes sense to you

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

So with javascript we declare the property we want to change but the transition duration still we declare it with CSS ?? Am I right ??

Kevin Korte
Kevin Korte
28,149 Points

Yes correct. I'm just adding a class that causes the css transition. I believe that CSS3 transitions are more performant than jQuery animations.