简体   繁体   English

将鼠标悬停在父div上时CSS淡入子级,然后将鼠标悬停在子代上时再次更改

[英]CSS fade children when hovering over parent div, then change again when hovering over child

I'm trying to get an effect like the one on the social media buttons on this website: http://www.goldsquare.co/about (the icons under the picture of the girl). 我正在尝试在此网站上的社交媒体按钮上获得类似的效果: http : //www.goldsquare.co/about (女孩图片下方的图标)。 My CSS now gets the buttons to fade to gray when I hover over the parent div, but the individual buttons don't turn black when I hover over them like they should. 现在,当我将鼠标悬停在父div上时,我的CSS的按钮将变为灰色,但是当我将鼠标悬停在其上时,各个按钮不会变成黑色。

HTML: HTML:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<div class="social-media-icons">
  <a href="#" class="fa fa-facebook"></a>
  <a href="#" class="fa fa-twitter"></a>
  <a href="#" class="fa fa-instagram"></a>
</div>

CSS: CSS:

.fa {
  padding: 10px;
  font-size: 15px;
  width: 20px;
  text-align: center;
  text-decoration: none;
  border-radius: 50%;
  background: black;
  color: white;
}

.fa:hover {
  transition: background 0.2 ease;
  background: black;
}


.social-media-icons:hover a {
  transition: background 0.2s ease;
  background: #cccccc;
}

You can see it in action at this jfiddle: http://jsfiddle.net/brettfisher/fcn4d097/8/ How do I get the desired effect? 您可以在以下jfiddle上看到它的运行情况: http : //jsfiddle.net/brettfisher/fcn4d097/8/如何获得所需的效果? Thank you in advance. 先感谢您。

By adding !important on .fa:hover . 通过.fa:hover添加 !important You could achieve the expected result. 您可以达到预期的结果。

Check the following fiddle: 检查以下小提琴:

 .fa { padding: 10px; font-size: 15px; width: 20px; text-align: center; text-decoration: none; border-radius: 50%; background: black; color: white; } .fa:hover { transition: background 0.2 ease; background: black !important; } .social-media-icons:hover a { transition: background 0.2s ease; background: #cccccc; } 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" /> <div class="social-media-icons"> <a href="#" class="fa fa-facebook"></a> <a href="#" class="fa fa-twitter"></a> <a href="#" class="fa fa-instagram"></a> </div> 

Read more about !important here 在此处阅读更多有关!important 信息

give !important to override .social-media-icons:hover a property 给!important重写.social-media-icons:hover a属性

 .fa { padding: 10px; font-size: 15px; width: 20px; text-align: center; text-decoration: none; border-radius: 50%; background: black; color: white; } .fa:hover { transition: background 0.2 ease!important; background: black!important; } .social-media-icons:hover a { transition: background 0.2s ease; background: #cccccc; } 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" /> <div class="social-media-icons"> <a href="#" class="fa fa-facebook"></a> <a href="#" class="fa fa-twitter"></a> <a href="#" class="fa fa-instagram"></a> </div> 

Try this one. 试试这个。

 .fa { padding: 10px; font-size: 15px; width: 20px; text-align: center; text-decoration: none; border-radius: 50%; background: black; color: white; } .social-media-icons:hover .fa{ background: #cccccc; } .social-media-icons:hover .fa:hover{ background: black; } 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" /> <div class="social-media-icons"> <a href="#" class="fa fa-facebook"></a> <a href="#" class="fa fa-twitter"></a> <a href="#" class="fa fa-instagram"></a> </div> 

You need to have a correct css specificity to let the specific one overwrite the other style. 您需要具有正确的CSS 特殊性,以使特定的CSS覆盖另一种样式。 (carefully to use !important ) (小心使用!important

Also, if you want to imitate more accurately, you need to move the transition outside the :hover style. 另外,如果您想更精确地模仿,则需要将transition移到:hover样式之外。

.fa {
  padding: 10px;
  font-size: 15px;
  width: 20px;
  text-align: center;
  text-decoration: none;
  border-radius: 50%;
  background: black;
  color: white;
  transition: background 0.2s ease;
}

.social-media-icons a.fa:hover {
  background: black;
}


.social-media-icons:hover a {
  background: #cccccc;
}

http://jsfiddle.net/0dpeaj1x/ http://jsfiddle.net/0dpeaj1x/

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

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