【问题标题】:CSS change icon color when input is focused输入焦点时CSS更改图标颜色
【发布时间】:2017-10-04 20:20:11
【问题描述】:

我希望用户图标和锁定图标在输入聚焦时改变颜色,但我不知道该怎么做,谁能帮助我(我希望图标颜色为#c0392b)

<div class="input-icons">
     <span class="fa fa-user"></span>
     <input type="text" placeholder="Username">
</div>

<div class="input-icons">
    <span class="fa fa-lock"></span>
    <input type="password" placeholder="Password">
</div>

CSS

@import url(https://fonts.googleapis.com/css?family=Lato:400,700,900);
@import "https://maxcdn.bootstrapcdn.com/font-awesome/4.6.0/css/font-     awesome.min.css";

input {
    padding: 5px;
    border-radius: 20px;
    border: 1px solid #cccccc;
    -webkit-box-shadow: inset 0px 0px 27px -8px rgba(0,0,0,0.75);
    -moz-box-shadow: inset 0px 0px 27px -8px rgba(0,0,0,0.75);
    box-shadow: inset 0px 0px 27px -8px rgba(0,0,0,0.75);
}

input:focus {
    outline: none;
    border: 1px solid #c0392b;
    color: c0392b;
}

input:focus > .fa-user {
    color: #c0392b;
}

.input-icons {
    position: relative;
    margin: 5px 100px 5px 100px;
}

.input-icons > input {
    text-indent: 17px;
    font-family: "Lato", sans-serif;
}

.input-icons > .fa-user {
  position: absolute;
  top: 7px;
  left: 7px;
  font-size: 15px;
color: #777777;
}

.input-icons > .fa-lock {
  position: absolute;
  top: 7px;
  left: 7px;
  font-size: 15px;
  color: #777777;
}

查看 JSFiddle 进行预览 JSFiddle

【问题讨论】:

    标签: css input icons focus


    【解决方案1】:

    您可以使用input:focus + .fa css 选择器来完成任务。请注意,我已将 &lt;span class="fa fa-user"&gt;&lt;/span&gt; 更改为输入元素之后。

    查看更多关于plus css selector

    更多关于css selectors

    updated fiddle here

    @import url(https://fonts.googleapis.com/css?family=Lato:400,700,900);
    @import "https://maxcdn.bootstrapcdn.com/font-awesome/4.6.0/css/font-awesome.min.css";
    
    input {
        padding: 5px;
        border-radius: 20px;
        border: 1px solid #cccccc;
        -webkit-box-shadow: inset 0px 0px 27px -8px rgba(0,0,0,0.75);
        -moz-box-shadow: inset 0px 0px 27px -8px rgba(0,0,0,0.75);
        box-shadow: inset 0px 0px 27px -8px rgba(0,0,0,0.75);
    }
    
    input:focus {
        outline: none;
        border: 1px solid #c0392b;
        color: c0392b;
    }
    
    input:focus + .fa {
        color: #c0392b;
    }
    
    .input-icons {
        position: relative;
        margin: 5px 100px 5px 100px;
    }
    
    .input-icons > input {
        text-indent: 17px;
        font-family: "Lato", sans-serif;
    }
    
    .input-icons > .fa-user {
      position: absolute;
      top: 7px;
      left: 7px;
      font-size: 15px;
      color: #777777;
    }
    
    .input-icons > .fa-lock {
      position: absolute;
      top: 7px;
      left: 7px;
      font-size: 15px;
      color: #777777;
    }
    
    <div class="input-icons">
         <input type="text" placeholder="Username">
         <span class="fa fa-user"></span>
    </div>
    
    <div class="input-icons">
        <input type="password" placeholder="Password">
        <span class="fa fa-lock"></span>
    </div>
    

    【讨论】:

    • 非常感谢。
    【解决方案2】:

    您的图标不在输入字段内,这就是您的 CSS 现在的描述方式。

    当你将图标 span 放在输入字段之后,你可以使用 + 选择器将 css 添加到以下元素。

    查看此链接https://www.w3.org/TR/CSS21/selector.html#adjacent-selectors

    【讨论】:

    • 感谢分享
    【解决方案3】:

    你可以试试这个

     <div class="input-pilotage">
       <input type="text">
       <label> date fin : </label>
       <i class="fi-calendar"></i>
     </div>
    
    //css
    .input-pilotage input:focus + label {
        color: #005EB8;
    }
    .input-pilotage input:focus + label + i {
        color: #005EB8;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 1970-01-01
      相关资源
      最近更新 更多