94

How do I use the search icon included in Font Awesome for input? I have a search feature on my site (based on PHPmotion), that I want to use for the search.

Here's the code:

<div id="search-bar">

      <form method="get" action="search.php" autocomplete="off" name="form_search">
        <input type="hidden" name="type" value="videos" />
            <input autocomplete="on" id="keyword" name="keyword" value="Search Videos" onclick="clickclear(this,
            'Search Videos')" onblur="clickrecall(this,'Search Videos')" style="font-family: verdana; font-weight:bold;
            font-size: 10pt; height: 28px; width:186px; color: #000000; padding-left: 2px; float:left; border: 1px solid black; background-color:
            #ffffff" />
            <input type="image" src="http://viddir.com/themes/default/images/search.jpg" height="30" width="30" border="0" style="float:right;"/>
        <div id="searchBoxSuggestions"></div>
        </form>
        </div>
Liam
  • 27,717
  • 28
  • 128
  • 190
user2275855
  • 1,127
  • 2
  • 10
  • 10

8 Answers8

118

You can use another tag instead of input and apply FontAwesome the normal way.

instead of your input with type image you can use this:

<i class="icon-search icon-2x"></i>

quick CSS:

.icon-search {
    color:white;
    background-color:black;
}

Here is a quick fiddle: DEMO

You can style it a little better and add event functionality, to the i object, which you can do by using a <button type="submit"> object instead of i, or with javascript.

The button sollution would be something like this:

<button type="submit" class="icon-search icon-large"></button>

And the CSS:

.icon-search {
    height:32px;
    width:32px;
    border: none;
    cursor: pointer;
    color:white;
    background-color:black;
    position:relative;
}

here is my fiddle updated with the button instead of i: DEMO


Update: Using FontAwesome on any tag

The problem with FontAwsome is that its stylesheet uses :before pseudo-elements to add the icons to an element - and pseudo elements don't work/are not allowed on input elements. This is why using FontAwesome the normal way will not work with input.

But there is a solution - you can use FontAwesome as a regular font like so:

CSS:

input[type="submit"] {
    font-family: FontAwesome;
}

HTML:

<input type="submit" class="search" value="&#xf002;" />

The glyphs can be passed as values of the value attribute. The ascii codes for the individual letters/icons can be found in the FontAwesome css file, you just need to change them into a HTML ascii number like \f002 to &#xf002; and it should work.

Link to the FontAwesome ascii code (cheatsheet): fortawesome.github.io/Font-Awesome/cheatsheet

The size of the icons can be easily adjusted via font-size.

See the above example using an input element in a jsfidde:

DEMO


Update: FontAwesome 5

With FontAwesome version 5 the CSS required for this solution has changed - the font family name has changed and the font weight must be specified:

input[type="submit"] {
    font-family: "Font Awesome 5 Free"; // for the open access version
    font-size: 1.3333333333333333em;
    font-weight: 900;
}

See @WillFastie 's comment with link to updated fiddle bellow. Thanks!

Community
  • 1
  • 1
Martin Turjak
  • 20,896
  • 5
  • 56
  • 76
  • question asked for input tag you answered for button tag – Web Designer cum Promoter Nov 12 '13 at 07:14
  • 2
    @HTMLDeveloper the problem is the way FontAwesome is normally applied ... as it uses `:before` and therefore won't work with `input` or `img` tags ... but there is a simple solution - namely, using FontAwesome as regular font ... see my updated solution above. I hope I earn the vote back ;-) – Martin Turjak Feb 20 '14 at 11:05
  • 11
    Boo on you @HTMLDeveloper...he clearly stated in his answer (in all edits, I checked) that you can't apply a Font Awesome style to an input tag. He then went on to post several different solutions and only one of which included using a button. You then proceeded to use his exact answer in your own. Bad form. – o_O Mar 01 '14 at 06:29
  • As someone who just finished implementing this, note that in a webkit environment, you need to also apply the CSS rule `-webkit-appearance: button` to your `` element, or else the font gets forced to a default. Plus, if you are setting the value with JS, you will need to decode the HTML entity; the perfect function is found here: http://stackoverflow.com/a/9609450/700471 – Matt Mc Mar 22 '14 at 03:27
  • 1
    Link to the *ascii code* (**cheatsheet**): http://fortawesome.github.io/Font-Awesome/cheatsheet/ – Vadorequest Apr 08 '14 at 13:50
  • 1
    @Vadorequest thanks! good idea ... I added the link to the answer ^_^ – Martin Turjak Apr 08 '14 at 13:55
  • 2
    +1 fkn huge trick!! thanks i was wondering why  wasn't showing in input thanks to you a realized it was to add the font-family:FontAwesome; you saved me :D – itsme Jun 15 '14 at 15:55
  • 1
    If you're trying to set this with javascript or jQuery, use the unicode variation: `$("input.search").val("\uf002");` – Jamie Chong Mar 27 '15 at 05:11
  • @MartinTurjak after applying font awesome the way how you showed 'placeholder' text got disappeared. How can I have placeholder's text back again? – Durdona A. Jul 18 '15 at 00:32
  • The placeholder will be ignored when you assign something to the `value` attribute. To add an image/icon to a text field, I would suggest placing another element that contains the icon over the textfield **[like so](http://jsfiddle.net/mturjak/m6p3dgto/1/)** – Martin Turjak Jul 19 '15 at 07:21
  • 1
    @MartinTurjak The CSS required has changed for FontAwesome 5 - the font name has changed and the font weight _must_ be specified. See the updated [Fiddle](http://jsfiddle.net/wfastie/m6hw2L4o/). – Will Fastie Aug 22 '18 at 06:50
  • @WillFastie Thanks for pointing this out. I added an update to the answer and referenced your comment for the fiddle. Thanks! – Martin Turjak Aug 22 '18 at 12:40
  • @MartinTurjak It's probably safer to mention all possible font family names in the CSS, including "FontAwesome." I have a suspicion that the Pro version of FA uses "Pro" instead of "Free" but it's not something I can confirm. It seems a fragile thing. I updated the Fiddle. – Will Fastie Aug 23 '18 at 14:36
  • "@WillFastie 's comment with link...", I couldn't find link in his comment, please check and clarify – vikramvi Jul 19 '22 at 10:49
50

Here is a solution that works with simple CSS and standard font awesome syntax, no need for unicode values, etc.

  1. Create an <input> tag followed by a standard <i> tag with the icon you need.

  2. Use relative positioning together with a higher layer order (z-index) and move the icon over and on top of the input field.

  3. (Optional) You can make the icon active, to perhaps submit the data, via standard JS.

See the three code snippets below for the HTML / CSS / JS.

Or the same in JSFiddle here: Example: http://jsfiddle.net/ethanpil/ws1g27y3/

$('#filtersubmit').click(function() {
  alert('Searching for ' + $('#filter').val());
});
#filtersubmit {
  position: relative;
  z-index: 1;
  left: -25px;
  top: 1px;
  color: #7B7B7B;
  cursor: pointer;
  width: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="filter" type="text" placeholder="Search" />
<i id="filtersubmit" class="fa fa-search"></i>
ethanpil
  • 2,522
  • 2
  • 24
  • 34
10

For those, who are wondering how to get FontAwesome icons to drupal input, you have to decode_entities first like so:

$form['submit'] = array(
'#type' => 'submit',
'#value' => decode_entities('&#xf014;'), // code for FontAwesome trash icon
// etc.
);
Tom Appleseed
  • 127
  • 1
  • 3
5

Change your input to a button element and you can use the Font Awesome classes on it. The alignment of the glyph isn't great in the demo, but you get the idea:

http://tinker.io/802b6/1

<div id="search-bar">
  <form method="get" action="search.php" autocomplete="off" name="form_search">
    <input type="hidden" name="type" value="videos" />
        <input autocomplete="on" id="keyword" name="keyword" value="Search Videos" onclick="clickclear(this,
        'Search Videos')" onblur="clickrecall(this,'Search Videos')" style="font-family: verdana; font-weight:bold;
        font-size: 10pt; height: 28px; width:186px; color: #000000; padding-left: 2px; border: 1px solid black; background-color:
        #ffffff" /><!--
        --><button class="icon-search">Search</button>
    <div id="searchBoxSuggestions"></div>
    </form>
</div>

#search-bar .icon-search {
    width: 30px;
    height: 30px;
    background: black;
    color: white;
    border: none;
    overflow: hidden;
    vertical-align: middle;
    padding: 0;
}

#search-bar .icon-search:before {
    display: inline-block;
    width: 30px;
    height: 30px;
}

The advantage here is that the form is still fully functional without having to add event handlers for elements that aren't buttons but look like one.

cimmanon
  • 67,211
  • 17
  • 165
  • 171
1

Similar to the top answer, I used the unicode character in the value= section of the HTML and called FontAwesome as the font family on that input element. The only thing I'll add that the top answer doesn't cover is that because my value element also had text inside it after the icon, changing the font family to FontAwesome made the regular text look bad. The solution was simply to change the CSS to include fallback fonts:

<input type="text" id="datepicker" placeholder="Change Date" value=" Sat Oct 19" readonly="readonly" class="hasDatepicker">

font-family: FontAwesome, Roboto, sans-serif;

This way, FontAwesome will grab the icon, but all non-icon text will have the desired font applied.

0
.fa-file-o {
    position: absolute;
    left: 50px;
    top: 15px;
    color: #ffffff
}

<div>
 <span class="fa fa-file-o"></span>
 <input type="button" name="" value="IMPORT FILE"/>
</div>
dxpkumar
  • 371
  • 2
  • 9
0

simple way for new font awesome

  <div class="input-group">
                    <input type="text" class="form-control" placeholder="Search" name="txtSearch"  >
                    <div class="input-group-btn">
                        <button class="btn btn-default" type="submit"><i class="fas fa-search"></i></button>
                    </div>
                </div>
Ali
  • 1,080
  • 16
  • 22
-4

to work this with unicode or fontawesome, you should add a span with class like below, because input tag not support pseudo classes like :after. this is not a direct solution

in html:

   <span class="button1 search"></span>
<input name="username">

in css:

.button1 {
    background-color: #B9D5AD;
    border-radius: 0.2em 0 0 0.2em;
    box-shadow: 1px 0 0 rgba(0, 0, 0, 0.5), 2px 0 0 rgba(255, 255, 255, 0.5); 
    pointer-events: none;
    margin:1px 12px;    
    border-radius: 0.2em;    
    color: #333333;
    cursor: pointer;
    position: absolute;
    padding: 3px;
    text-decoration: none;   

}
  • 7
    question asked for input tag you answered for span tag – o_O Mar 01 '14 at 06:29
  • Actually, the only answer about applying to text inputs instead of buttons, so +1. – BorisOkunskiy Jun 28 '14 at 08:58
  • 1
    @incarnate actually this answer does not apply the style to an input field but to a span, whereas [**MartinTurjak** offers a complete solution (and additional alternatives) in his **answer above**](http://stackoverflow.com/a/15988499/2113185). – BobbyZ Sep 07 '14 at 19:55
  • I really meant _text_ inputs here since the subject states «How do I add a Font Awesome icon to input field?» — and mentions nothing about buttons. MartinTurjak describes good ways to style buttons, not text fields. – BorisOkunskiy Oct 28 '14 at 08:30