当输入获得焦点时 CSS 改变图标颜色

DeC*_*roY 2 css icons focus input

我希望用户图标和锁定图标在输入聚焦时改变颜色,但我不知道该怎么做,有人可以帮助我吗(我希望图标颜色为 #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>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

查看 JSFiddle 预览 JSFiddle

Emi*_*len 5

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

查看有关plus css 选择器的更多信息

有关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 {
    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>
Run Code Online (Sandbox Code Playgroud)