Skip to content Skip to sidebar Skip to footer

How To Get Font Awesome Icon Into Input Type Text

I have trying for font awesome icon into text input type. But can get idea and it's not working. It will be like in below image.

Solution 1:

You could try this :

    p{
      position: relative;
      font-family: 'FontAwesome';
    }
    p::after{
        position: relative;
        left: -20px;
        content: "\f002";
    }
    input{
      padding-right: 20px;
    }
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<p>
     <input type="text" class="search_location" value="Choose Location">
    </p>

Where content is your icon unicode. You'll have to specify the font-family as well so it uses font awesome (unicodes are found on their icon page).

https://jsfiddle.net/h6877894/2/


Solution 2:

To place an element with position: absolute usually we need to add position: relative on parent element. As from Documentation:

The absolutely positioned element is positioned relative to nearest positioned ancestor (non-static). If a positioned ancestor doesn't exist, the initial container is used.

I would suggest you to use a bit modified HTML as shown below:

<div class="input-holder">
  <input type="text" class="search_location" value="Choose Location">
  <i class="fa fa-map-marker icon"></i>
</div>

With some following changes in css:

* {box-sizing: border-box;}

.input-holder {
  position: relative;
  width: 300px;
}

.search_location {
  display: block;
  width: 100%;
}

* {box-sizing: border-box;}
.input-holder {
  position: relative;
  width: 300px;
}
.search_location {
    padding: 9px 10px 8px 10px;
    background: #fff;
    display: block;
    color: #777;
    width: 100%;
}
.icon { 
  position: absolute;
  right: 10px;
  top: 10px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<div class="input-holder">
  <input type="text" class="search_location" value="Choose Location">
  <i class="fa fa-map-marker icon"></i>
</div>

Post a Comment for "How To Get Font Awesome Icon Into Input Type Text"