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 trialAleksandrs Karevs
Courses Plus Student 11,178 PointsChange style of Paragraph <p> when user hovers over an image above.
Hi everyone,
Long story short, I've got a section on my website, where 3 images are shown in a row with caption written beneath these images using Paragraph <p>, like shown here: http://d.pr/i/1ftbB .
Now, I would like to make it so that when a user hovers (:hover) over the image, the caption below the image should change its colour to: #E48210.
So far I have managed to achieve the effect, where when the user hovers over the caption itself, it changes its colour, like so: http://d.pr/v/1kpSA .
I have the following HTML code for this section:
<div id="wrapper">
<section>
<ul id="service">
<li><a href=""><img src="images/services/service-1.png" alt="автомобильные перевозки"><p>Автомобильные</br>перевозки</p></a></li>
<li><a href=""><img src="images/services/service-2.png" alt="железнодорожные перевозки"><p>Железнодорожныe</br>перевозки</p></a></li>
<li><a href=""><img src="images/services/service-3.png" alt="морские перевозки"><p>Морские</br>перевозки</p></a></li>
<li><a href=""><img src="images/services/service-4.png" alt="таможенные услуги"><p>Таможенные</br>услуги</p></a></li>
<li><a href=""><img src="images/services/service-5.png" alt="сборные грузы и складирование"><p>Сборные грузы</br>и складирование</p></a></li>
<li><a href=""><img src="images/services/service-6.png" alt="перевозка опасных грузов"><p>Перевозка</br>опасных грузов</p></a></li>
</ul>
</section>
</div>
And here is my CSS for this section:
#service p:hover {
color: #E48210;
}
So, the question is, how do I write CSS, which would do the following:
- Change the color of Paragraph below Image to #E48210 when user :hovers over Image.
Thank you very much in advance!
Any help would be highly appreciated.
2 Answers
Lucas Santos
19,315 PointsVery simple example:
HTML
<div class="container">
<img src="http://s.hswstatic.com/gif/landscape-photography-1.jpg" alt="">
<p>THIS IS SOME TEXT!</p>
</div>
CSS
.container:hover p{
color: green;
}
The .container is the div holding both your image and your paragraph tag. Simply change your code to fit your needs.
onuniku
5,394 PointsInstead of using id's (service-1, service-2 > service-6) use class'es.
<div id="wrapper"> <section> <ul id="service"> <li class="service-main"><a href=""><img src="images/services/service-1.png" alt="автомобильные перевозки"><p>Автомобильные</br>перевозки</p></a></li> <li class="service-main"><a href=""><img src="images/services/service-2.png" alt="железнодорожные перевозки"><p>Железнодорожныe</br>перевозки</p></a></li> <li class="service-main"><a href=""><img src="images/services/service-3.png" alt="морские перевозки"><p>Морские</br>перевозки</p></a></li> <li class="service-main"><a href=""><img src="images/services/service-4.png" alt="таможенные услуги"><p>Таможенные</br>услуги</p></a></li> <li class="service-main"><a href=""><img src="images/services/service-5.png" alt="сборные грузы и складирование"><p>Сборные грузы</br>и складирование</p></a></li> <li class="service-main"><a href=""><img src="images/services/service-6.png" alt="перевозка опасных грузов"><p>Перевозка</br>опасных грузов</p></a></li> </ul> </section> </div>
CSS: .service-main: hover p { color: #E48210; -o-transition:.3s; -ms-transition:.3s; -moz-transition:.3s; -webkit-transition:.3s; transition:.3s; }
But i don't see your paragraph in the code :)
Aleksandrs Karevs
Courses Plus Student 11,178 PointsAleksandrs Karevs
Courses Plus Student 11,178 PointsAwesome. Thanks. No idea why I haven't thought about it at the first place, but thanks a lot for the help!
Lucas Santos
19,315 PointsLucas Santos
19,315 PointsNo problem
Aleksandrs Karevs
Courses Plus Student 11,178 PointsAleksandrs Karevs
Courses Plus Student 11,178 PointsLucas,
One more question. It all works now, however, it works in a way that when I hover over any image, all Captions change their Color to #E48210, whereas I would like only a certain Caption (the one below the Image) to change its Colour.
Here's my CSS:
As far as I understand, I need to add unique id's to each of li's in my html:
Or maybe there is a better way of doing it? Any suggestions?
Lucas Santos
19,315 PointsLucas Santos
19,315 PointsAleksandrs Karevs Just use li instead of #service like this:
or if you wanted to be more specific do:
Aleksandrs Karevs
Courses Plus Student 11,178 PointsAleksandrs Karevs
Courses Plus Student 11,178 PointsI have managed to do it the following way:
My html now looks like this:
i.e. I have added unique id's to each of the services listed on my website.
and this is how my CSS looks like now:
Not sure, maybe there's a better way of doing it, but at least it works :)
Aleksandrs Karevs
Courses Plus Student 11,178 PointsAleksandrs Karevs
Courses Plus Student 11,178 PointsAwesome. Your method is definitely better ;) Thanks!