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
Pedro Lucas Da Silva Cunha
5,276 PointsNeed help with selectors
I just started a website that acts as a portfolio for my skills in CSS, photoshop, and HTML. I'm trying to create a hover effect in the nav menu. I have an image of a spider under the nav bar and I want it to move down the page a little when the nav bar links are hovered over. This is the code I was thinking of using:
.nav:hover { -webkit-transition: translateY(100%); }
The website url is www.spinsomewebs.com for those of you that want a visual representation(my explanations are usually pretty confusing). Is there any way that I can do this in CSS? The problem I'm facing now is that the code selects the whole .nav div. The div with the class .nav holds the ul and the image. Is there any way to transition the image but trigger the transition only upon hovering the links in the nav bar?
4 Answers
James Barnett
39,199 PointsThere's no really good way to do it in CSS. If you wanted to hack it you could re-structure your markup so your spider and links are siblings.
<ul>
<li class = "nav">home</li>
<li class = "nav">about</li>
<li class = "pic"></li>
<li class = "line"></li>
</ul>
Now you can use the general sibling selector ~
.nav:hover ~ .line { height: 60%;}
.nav:hover ~ ul{ height: 100px; }
.nav:hover ~ .pic{ top: 100px; }
Here's a quick demo: http://codepen.io/jamesbarnett/pen/cJvAm
John Locke
15,479 PointsPedro:
The way I would solve this is create a CSS class that has the transition. Use jQuery to addClass('youreffect') to the image when the mouse enters the nav div or hovers over it.
http://api.jquery.com/mouseover/
http://api.jquery.com/mouseleave/
http://api.jquery.com/mouseenter/
Carman A
7,672 PointsYou might be able to use the + selector so it could be like .nav li:hover + .spider { margin-top: 20px; } .
James Barnett
39,199 PointsThe + combinator only selects adjacent elements. So it would only work for the 1 li next to the .spider.
Pedro Lucas Da Silva Cunha
5,276 PointsWow James, awesome. That's exactly what I was looking for. I really appreciate everyone's input. I'll see how it looks on the website. Thank you all for your input.