Put icon inside input element in a form

793,964

Solution 1

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;

Solution 2

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.

Solution 3

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">

Solution 4

You can try this:

input[type='text'] {
    background-image: url(images/comment-author.gif);
    background-position: 7px 7px;
    background-repeat: no-repeat;
}

Solution 5

I find this the best and cleanest solution to it. Using text-indent on the input element:

#icon {
  background-image: url(../images/icons/dollar.png);
  background-repeat: no-repeat;
  background-position: 2px 3px;
}
<input id="icon" style="text-indent:17px;" type="text" placeholder="Username" />

Share:
793,964
akif
Author by

akif

Updated on January 15, 2022

Comments

  • akif
    akif over 2 years

    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

  • Ross
    Ross over 12 years
    a good addition to this answer. sometimes ie gives problems and this is a good way to solve one of them. worked for me albeit with a span instead.
  • QMaster
    QMaster over 10 years
    Could you please describe more about top and left parameters (7px 7 px) and other attributes? That is must be helpful.
  • Francisco Corrales Morales
    Francisco Corrales Morales about 10 years
    the padding-left is being ignored by Internet Explorer 8. Well not really ignored, but as soon as I type a long input, the input begins to appear in front of the icon.
  • a coder
    a coder about 9 years
    Advice: stop using and supporting IE 8.
  • ishanbakshi
    ishanbakshi over 8 years
    how can I align the image to the right end of the input field?
  • Cătălin Rădoi
    Cătălin Rădoi about 8 years
    The problem with background images is that Chrome's autofill removes the background image and makes the background yellow completely
  • Kostiantyn
    Kostiantyn over 7 years
    I'm supporting text-indent because padding-left will increase field's width and it'll overflow out of the parent element.
  • heroxav
    heroxav almost 7 years
    Nice way to add more complex structures inside inputs!
  • harpo
    harpo almost 7 years
    @jonhue, I wouldn't recommend this unless you're still living in the previous decade and need to support IE6. Even then, I wouldn't call it "clean."
  • heroxav
    heroxav almost 7 years
    @harpo How would you include links inside inputs then?
  • Wylliam Judd
    Wylliam Judd over 6 years
    why did you put the style inline instead of in the #icon css?
  • Rusty
    Rusty over 6 years
    @WylliamJudd That is just for demonstration purpose :)
  • Vyshnia
    Vyshnia about 6 years
    Seems that ::before can not be applied of input element.
  • ejntaylor
    ejntaylor about 6 years
    To align to the right use: background-position: right;
  • neptune
    neptune over 5 years
    the image disappear when autocompleting on chrome
  • user460114
    user460114 over 5 years
    How do you make the image clickable, e.g. if using a "save" icon?
  • Arjun
    Arjun about 5 years
    do you know how to set click event only for this image.?
  • borbulon
    borbulon over 4 years
    This is super old and I hope maybe you've learned since over a year ago, but background is a shorthand property name which you use to cover all the background properties at once: background: [color] [image url] [repeat] [position], and that's why your color was overwriting everything. You could also have left it in place and renamed the property to background-color
  • fantja
    fantja over 4 years
    @borbulon yes you are right. it's almost always best to use shorthand property name, totally agree on that. But purpose of this post is to point out that order of properties sometimes matters, and those 'small' CSS rules can be frustrating a lot, especially to newcomers in development.
  • shivam srivastava
    shivam srivastava over 4 years
    It worked for me for my hybrid app, I just made the whole input box clickable after inserting image.
  • Mario Werner
    Mario Werner over 4 years
    Clean and simple, yes. You should also remove the input border with input { border: none; } and add the border to #input-container { border: 1px solid #000; } to make it look like the image is inside and actually part of the input.
  • The Fool
    The Fool about 4 years
    @MarioWerner he pushed the image inside the input, no need to do border hacks, that's the beauty. I am going to use the idea.
  • borbulon
    borbulon almost 4 years
    Totally agree, but the point still stands: background is a shorthand property name. A better fix would have been to not use the shorthand, but use background-color for the color property (assuming you want both a color and an image).
  • majid savalanpour
    majid savalanpour almost 4 years
    why did you use id instead of class?
  • G0BLiN
    G0BLiN almost 4 years
    A minor issue with this - the image is positioned over (z axis) the input, so it blocks clicks over it from focusing on the input (this can be observed in the snippet). It's possible to resolve by sending the image under the input
  • d0rf47
    d0rf47 over 3 years
    This is an excellent comprehensive answer! saved me much time thank you for this
  • Jasur Kurbanov
    Jasur Kurbanov about 3 years
    can I use your code both for personal and commercial projects ?
  • Sheldon Oliveira
    Sheldon Oliveira almost 3 years
    to add it on the right with some space background-position: center; background-position-x: calc(100% - 12px);
  • Tiago Rangel de Sousa
    Tiago Rangel de Sousa almost 3 years
    Not buttons, inputs.
  • Tiago Rangel de Sousa
    Tiago Rangel de Sousa almost 3 years
    Good solution, but please add an explanation for your solution.
  • Tiago Rangel de Sousa
    Tiago Rangel de Sousa almost 3 years
    Good solution, but please add an explanation for your solution.
  • yazabara
    yazabara almost 3 years
    I don't remember it now =) I've lost UI experience ... 2015...backend only..)
  • CHANist
    CHANist about 2 years
    seems this css works only on non-input element, like div.