Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put a font-awesome icon inside an input tag

I'm trying to put a Font-awesome icon inside an input tag.

This is my HTML code:

<div class="input-group">
        <input class="form-control" type="password" placeholder="Password">
        <span class="input-group-addon"><i class="fa fa-key fa-fw"></i></span>
</div>

But that icon is bigger than input box as you can see in this picture:

enter image description here

How can I fix the icon?

like image 992
DistribuzioneGaussiana Avatar asked Oct 10 '16 13:10

DistribuzioneGaussiana


People also ask

How do I put a Font Awesome icon inside an input?

Place Font Awesome Icon Inside Input. In HTML, create a div element with a class name "inputWithIcon", place your input and Font Awesome icon inside it as follows: If you want to set a background color for the icon, just add a class name "inputIconBg" to the input wrapper.

How to use Font-Awesome icons with CSS?

Example: In this example, we will take a form where the input field is necessary. After that, we will place the font-awesome icon inside the input field. We will use the CDN link to use the font-awesome icons. After the input field, we will place our icon. Then with the help of CSS position property, we will place the icon over the input field.

How to put icon inside an input element in a form?

CSS to put icon inside an input element in a form. To add icon inside the input element the <i> tag and <span> tag are used widely to add icons on the webpages. To add any icons on the webpages or in some specific area, it needs the fontawesome link inside the head tag.

How do I include Font Awesome in my app or page?

You can specify their size using pixels, and they will assume the font size of their parent HTML elements. To include Font Awesome in your app or page, just add the following code to the <head> element at the top of your HTML: The i element was originally used to make other elements italic, but is now commonly used for icons.


2 Answers

Bootstrap 3

Checkout the Bootstrap examples in the Font Awesome documentation:

<div class="input-group margin-bottom-sm">
  <span class="input-group-addon"><i class="fa fa-envelope-o fa-fw"></i></span>
  <input class="form-control" type="text" placeholder="Email address">
</div>
<div class="input-group">
  <span class="input-group-addon"><i class="fa fa-key fa-fw"></i></span>
  <input class="form-control" type="password" placeholder="Password">
</div>

It should work out of the box, so if you still have height differences, check that there is not other CSS stuff that override your input-group-addon height

Working JSFiddle: https://jsfiddle.net/vs0wpy80

Bootstrap 4

Bootstrap 4 introduced some new classes for input groups, we need to use input-group-prepend and input-group-append:

<div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text"><i class="fa fa-envelope-o fa-fw"></i></span>
  </div>
  <input class="form-control" type="text" placeholder="Email address">
</div>

<div class="input-group mb-3">
  <input class="form-control" type="text" placeholder="Email address">
  <div class="input-group-append">
    <span class="input-group-text"><i class="fa fa-envelope-o fa-fw"></i></span>
  </div>
</div>

Working JSFiddle: https://jsfiddle.net/8do9v4dp/5/

like image 197
Finrod Avatar answered Sep 28 '22 18:09

Finrod


input.form-control{
    margin-top: 0;
}

In my case it helps

like image 43
Alan Mroczek Avatar answered Sep 28 '22 19:09

Alan Mroczek