CSS change icon color when input is focused
You can use input:focus + .fa
css selector do accomplish the task. Notice that I've changed the <span class="fa fa-user"></span>
to come after input element.
See more about the plus css selector
More about css selectors
See 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>
CSS change icon color when input is focused
You can use input:focus + .fa
css selector do accomplish the task. Notice that I've changed the <span class="fa fa-user"></span>
to come after input element.
See more about the plus css selector
More about css selectors
See 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>
Related Topics
Background Size Cover Ie11 Not Covering Full Area
Vertical and Horizontal Alignment of Text with CSS
Background-Size Transition on Hover Causes Chrome to "Shake" Background Image
How to Fix Safari Mix-Blend-Mode: Color-Dodge Bug
Add Cell Borders in an R Datatable
Multiple Attributes in Ng-Style
How to Set The Width and Height of a Textarea Using CSS
Vertically Aligning an Image to The Bottom Inside a Bootstrap "Column"
CSS: Table and Table-Cell Replacement for Ie7
CSS - Background Images Not Displaying Properly on Mobile Browsers
Selenium Webdriver CSS Selector Help - for Selecting Date
How to Create a Child Theme in Wordpress
How to Break Lines in Urls in Stylesheet
Is There a List of Browser Conditionals for Use Including Stylesheets
Why Does The Img Tag Accept The Margin-Top Property
Colored Characters in HTML Title