简体   繁体   English

更改图像悬停时的文本颜色

[英]Change text color on image hover

Thanks to great guy I've discovered today possibility of changing color of the text when you hover of the div in that way: 感谢伟大的家伙,我今天发现当你以这种方式悬停div时,可能会改变文本的颜色:

 div.button:hover span { color: rgb(88, 202, 230); } 
  <div class='button'> <div class='svg bookmarks'></div> <span class=''>Bookmarks</span> </div> 

And a bit later I've tried to implement that kind of functionality on top of the image, but the result is not achieved, can you please explain what is done wrong and maybe the difference between cases. 稍后我试图在图像上实现这种功能,但结果没有实现,请你解释一下做错了什么,也许是案例之间的区别。

  img.button:hover p.text { color: rgb(88, 202, 230); } p { font-weight: 300; transition: color 1s ease; } 
  <img class='button' src='http://s8.postimg.org/s9vmeo1dx/user_avatar_circle.jpg'> <p class='text'>Profile</p> 

Use + CSS selector: 使用+ CSS选择器:

  img.button:hover + p.text { color: rgb(88, 202, 230); } p { font-weight: 300; transition: color 1s ease; } 
  <img class='button' src='http://s8.postimg.org/s9vmeo1dx/user_avatar_circle.jpg'> <p class='text'>Profile</p> 

try adding ~ 尝试添加〜

  img.button:hover ~ p.text { color: rgb(88, 202, 230); } p { font-weight: 300; transition: color 1s ease; } 
  <img class='button' src='http://s8.postimg.org/s9vmeo1dx/user_avatar_circle.jpg'> <p class='text'>Profile</p> 

In a CSS selector, a space mean "is a child of", so the way your selector is working says when an img tag with class "button" is hovered over, select the child p tag with class text and change the color. 在CSS选择器中,空格意​​味着“是孩子的”,所以选择器工作的方式是当一个带有“按钮”类的img标签悬停在上面时,选择带有类文本的子p标签并更改颜色。

Instead use the adjacent sibling selector (+) to say that it is a sibling and not a child 而是使用相邻的兄弟选择器(+)来表示它是兄弟而不是孩子

  img.button:hover + p.text { color: rgb(88, 202, 230); } p { font-weight: 300; transition: color 1s ease; } 
  <img class='button' src='http://s8.postimg.org/s9vmeo1dx/user_avatar_circle.jpg'> <p class='text'>Profile</p> 

Your selector is wrong because that paragraph is not a child of the image. 您的选择器错误,因为该段落不是图像的子项。 Using a common container would fix it: 使用通用容器可以修复它:

 .c:hover p.text { color: rgb(88, 202, 230); } p { font-weight: 300; transition: color 1s ease; } 
 <div class="c"> <img class='button' src='http://s8.postimg.org/s9vmeo1dx/user_avatar_circle.jpg'> <p class='text'>Profile</p> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM