鼠标悬停时更改font-awesome图标颜色

时间:2016-10-21 11:56:11

标签: html css icons font-awesome

我在输入文本框中有图标。当鼠标悬停在我想要的图标上时 改变了图标的颜色。我尝试了但是没有工作。

在我的代码下面。

<div class="row">
        <span class="fa fa-envelope" style="position: absolute; top: 10px; left: -5px; color: black"></span>
        <span class="fa fa-file" style="position: absolute; top: 10px; left: 20px; color: black"></span>
        <span class="fa fa-calendar" style="position: absolute; top: 10px; left: 42px; color: black"></span>
        <span class="fa fa-bar-chart" style="position: absolute; top: 10px; left: 65px; color: black"></span>
        <input id="Text1" class="col-md-8" type="text" placeholder="Send Message" style="padding: 30px; border: 1px solid #aaa />
    </div>

我需要解决这个问题。

2 个答案:

答案 0 :(得分:4)

使用内联样式意味着您必须使用!important覆盖默认状态black

&#13;
&#13;
span:hover {
  color: green !important;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>

<div class="row">
        <span class="fa fa-envelope" style="position: absolute; top: 10px; left: -5px; color: black"></span>
        <span class="fa fa-file" style="position: absolute; top: 10px; left: 20px; color: black"></span>
        <span class="fa fa-calendar" style="position: absolute; top: 10px; left: 42px; color: black"></span>
        <span class="fa fa-bar-chart" style="position: absolute; top: 10px; left: 65px; color: black"></span>
        <input id="Text1" class="col-md-8" type="text" placeholder="Send Message" style="padding: 30px; border: 1px solid #aaa />
    </div>
&#13;
&#13;
&#13;

更好的解决方案可能如下所示:

&#13;
&#13;
.fa {
  position: absolute;
  top: 10px;
  color: black;
}
.fa:nth-child(1) {
  left: -5px;
}
.fa:nth-child(2) {
  left: 20px;
}
.fa:nth-child(3) {
  left: 42px;
}
.fa:nth-child(4) {
  left: 65px;
}
.fa:hover {
  color: green;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />

<div class="row">
  <span class="fa fa-envelope"></span>
  <span class="fa fa-file"></span>
  <span class="fa fa-calendar"></span>
  <span class="fa fa-bar-chart"></span>
  <input id="Text1" class="col-md-8" type="text" placeholder="Send Message" style="padding: 30px; border: 1px solid #aaa" />
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

我建议删除内联color: black并将其替换为css样式。尽可能避免使用!important,对吗?

.fa{
  color: black
}
.fa:hover{
  color:blue;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.css" rel="stylesheet"/>
<div class="row">
        <span class="fa fa-envelope" style="position: absolute; top: 10px; left: -5px;"></span>
        <span class="fa fa-file" style="position: absolute; top: 10px; left: 20px;"></span>
        <span class="fa fa-calendar" style="position: absolute; top: 10px; left: 42px;"></span>
        <span class="fa fa-bar-chart" style="position: absolute; top: 10px; left: 65px;"></span>
        <input id="Text1" class="col-md-8" type="text" placeholder="Send Message" style="padding: 30px; border: 1px solid #aaa />
    </div>