Font Awesome icon inside text input element
You're right. :before and :after pseudo content is not intended to work on replaced content like img
and input
elements. Adding a wrapping element and declare a font-family is one of the possibilities, as is using a background image. Or maybe a html5 placeholder text fits your needs:
<input name="username" placeholder="">
Browsers that don’t support the placeholder attribute will simply ignore it.
UPDATE
The before content selector selects the input: input[type="text"]:before
. You should select the wrapper: .wrapper:before
. See http://jsfiddle.net/allcaps/gA4rx/ .
I also added the placeholder suggestion where the wrapper is redundant.
.wrapper input[type="text"] {
position: relative;
}
input { font-family: 'FontAwesome'; } /* This is for the placeholder */
.wrapper:before {
font-family: 'FontAwesome';
color:red;
position: relative;
left: -5px;
content: "\f007";
}
<p class="wrapper"><input placeholder=" Username"></p>
Fallback
Font Awesome uses the Unicode Private Use Area (PUA) to store icons. Other characters are not present and fall back to the browser default. That should be the same as any other input. If you define a font on input elements, then supply the same font as fallback for situations where us use an icon. Like this:
input { font-family: 'FontAwesome', YourFont; }
Output:
HTML:
<input name="txtName" id="txtName">
<span class="fa fa-info-circle errspan"></span>
CSS:
<style type="text/css">
.errspan {
float: right;
margin-right: 6px;
margin-top: -20px;
position: relative;
z-index: 2;
color: red;
}
</style>
(Or)
Output:
HTML:
<div class="input-wrapper">
<input type="text" />
</div>
CSS:
<style type="text/css">
.input-wrapper {
display:inline-block;
position: relative
}
.input-wrapper:after {
font-family: 'FontAwesome';
content: '\f274';
position: absolute;
right: 6px;
}
</style>
This answer will work for you if you need the following conditions met (none of the current answers met these conditions):
- The icon is inside the text box
- The icon shouldn't disappear when text is entered into the input, and text entered goes to the right of the icon
- Clicking the icon should bring the underlying input into focus
I believe that 3 is the minimal number of HTML elements to satisfy these conditions:
.input-icon{
position: absolute;
left: 3px;
top: calc(50% - 0.5em); /* Keep icon in center of input, regardless of the input height */
}
input{
padding-left: 17px;
}
.input-wrapper{
position: relative;
}
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"/>
<div class="input-wrapper">
<input id="stuff">
<label for="stuff" class="fa fa-user input-icon"></label>
</div>
You could use a wrapper. Inside the wrapper, add the font awesome element i
and the input
element.
<div class="wrapper">
<i class="fa fa-icon"></i>
<input type="button">
</div>
then set the wrapper's position to relative:
.wrapper { position: relative; }
and then set the i
element's position to absolute, and set the correct place for it:
i.fa-icon { position: absolute; top: 10px; left: 50px; }
(It's a hack, I know, but it gets the job done.)