Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 image icon to input placeholder

Tags:

I'd like to add an image icon to an input placeholder, like in this picture: enter image description here

Please note that this is a placeholder, so when the user starts typing, the icon also disappears.

I came with the following solution for webkit (Safari+Chrome) using ::-webkit-input-placeholder and ::before. Unfortunately, the straightforward application to mozilla seems not to work.

So my question is: is there a cross-browser solution to add an image icon to an input placeholder?

Solution for webkit:

#myinput::-webkit-input-placeholder::before {      content: ' ';     position: absolute;     top: 2px; /* adjust icon position */     left: 0px;     width: 14px; /* size of a single icon */     height: 14px;     background-image: url("/path/to/icons.png"); /* all icons in a single file */     background-repeat: no-repeat;     background-position: -48px 0px; /* position of the right icon */ } 
like image 403
ecesena Avatar asked Dec 07 '12 10:12

ecesena


People also ask

Can you put an image as a placeholder in HTML?

To place an image in an HTML document, all you have to do is mention the width and height in the URL of Unsplash It and then use that as the src value of your image element.

Can I put icon in placeholder?

You can't add an icon and text because you can't apply a different font to part of a placeholder, however, if you are satisfied with just an icon then it can work.

Can I put an image in placeholder?

To add an image from our Photo Library, select the “Image Placeholder” and click on the “Photos” tab to search for an image. The desired image will be automatically added by simply clicking on it.

How do I add an icon to my input box?

In HTML, icons are added with the <i> tag. For it to be added inside the input elements, it must be added between the closing and opening tags of the elements in which you want the icon to be displayed.


2 Answers

  1. You can set it as background-image and use text-indent or a padding to shift the text to the right.
  2. You can break it up into two elements.

Honestly, I would avoid usage of HTML5/CSS3 without a good fallback. There are just too many people using old browsers that don't support all the new fancy stuff. It will take a while before we can drop the fallback, unfortunately :(

The first method I mentioned is the safest and easiest. Both ways requires Javascript to hide the icon.

CSS:

input#search {     background-image: url(bg.jpg);     background-repeat: no-repeat;     text-indent: 20px; } 

HTML:

<input type="text" id="search" name="search" onchange="hideIcon(this);" value="search" /> 

Javascript:

function hideIcon(self) {     self.style.backgroundImage = 'none'; } 

September 25h, 2013

I can't believe I said "Both ways requires JavaScript to hide the icon.", because this is not entirely true.

The most common timing to hide placeholder text is on change, as suggested in this answer. For icons however it's okay to hide them on focus which can be done in CSS with the active pseudo-class.

#search:active { background-image: none; } 

Heck, using CSS3 you can make it fade away!

http://jsfiddle.net/2tTxE/


November 5th, 2013

Of course, there's the CSS3 ::before pseudo-elements too. Beware of browser support though!

            Chrome  Firefox     IE      Opera   Safari :before     (yes)   1.0         8.0     4       4.0 ::before    (yes)   1.5         9.0     7       4.0 

https://developer.mozilla.org/en-US/docs/Web/CSS/::before

like image 66
Tim S. Avatar answered Sep 21 '22 05:09

Tim S.


`CSS:

input#search{  background-image: url(bg.jpg);  background-repeat: no-repeat;  text-indent: 20px; }  input#search:focus{  background-image:none; } 

HTML:

<input type="text" id="search" name="search" value="search" />` 
like image 21
Shumail Rafique Avatar answered Sep 19 '22 05:09

Shumail Rafique