Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put icon inside input element in a form

How do I put an icon inside a form's input element?

Screenshot of a web form with three inputs which have icons in them

Live version at: Tidal Force theme

like image 904
akif Avatar asked May 27 '09 19:05

akif


People also ask

How do you put an icon in HTML?

To insert an icon, add the name of the icon class to any inline HTML element. The <i> and <span> elements are widely used to add icons. All the icons in the icon libraries below, are scalable vector icons that can be customized with CSS (size, color, shadow, etc.)

How do I add font awesome icon inside TextBox?

The font-awesome icon can be placed by using the fa prefix before the icon's name. 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.

Can you put elements inside input?

No, you cannot have any element contained within an <input> element. It is an empty (void) element, and the closing tag must be ommitted. Thus you cannot have any element contained within it.


3 Answers

The site you linked uses a combination of CSS tricks to pull this off. First, it uses a background-image for the <input> element. Then, in order to push the cursor over, it uses padding-left.

In other words, they have these two CSS rules:

background: url(images/comment-author.gif) no-repeat scroll 7px 7px;
padding-left:30px;
like image 52
Dan Lew Avatar answered Oct 18 '22 23:10

Dan Lew


The CSS solutions posted by others are the best way to accomplish this.

If that should give you any problems (read IE6), you can also use a borderless input inside of a div.

<div style="border: 1px solid #DDD;">
    <img src="icon.png"/>
    <input style="border: none;"/>
</div>

Not as "clean", but should work on older browsers.

like image 70
harpo Avatar answered Oct 18 '22 22:10

harpo


A solution without background-images:

.icon {
  padding-left: 25px;
  background: url("https://static.thenounproject.com/png/101791-200.png") no-repeat left;
  background-size: 20px;
}
<input type="text" class="icon" value placeholder="Search">

Or for right to left icon

.icon-rtl {
  padding-right: 25px;
  background: url("https://static.thenounproject.com/png/101791-200.png") no-repeat right;
  background-size: 20px;
}
<input type="text" class="icon-rtl" value placeholder="Search">
like image 64
majid savalanpour Avatar answered Oct 19 '22 00:10

majid savalanpour