-2

i have use the following code snippet

<input type="text" class="inputtext" style="margin: 2px;" placeholder="Search">

i have own font icons, i want to display the icon in the input through placeholder. How to use the content to display the icon in the input

I have refer the following stack overflow link

stackoverflow link

but i can not achieve in my case . Is there any way to achieve this or any other documentation link for display font icon in input?

Community
  • 1
  • 1
Raja
  • 209
  • 1
  • 7
  • 16

2 Answers2

1

Assuming you're talking about adding a glyph icon and not a background image to your input, you will need to use either the :before or :after pseudo elements to inject and position one, howwever note that these are not available for input elements, so you will need to wrap your input within e.g. a span, then apply styling to this to add your placeholder icon. In the below example, I've simply added a small magnifying glass to the right hand side, though you can edit as appropriate.

Demo Fiddle (icon on right)

HTML

<span class='search'><input type="text" class="inputtext" style="margin: 2px;" placeholder="Search" /></span>

CSS

.search{
    display:inline-block;  
    position:relative;
}
.search:after{
    display:inline-block;
    position:absolute;
    right:5px;
    top:5px;
    color:grey;
    font-family:fontAwesome;
    content:'\f002';
}

Demo Fiddle (icon on left)

If you want the icon to appear on the left of the input, you need some additional CSS trickery:

input{
    border:none;
    padding-left:20px;
}
.search {
    display:inline-block;
    position:relative;
    border:1px solid grey;
}
.search:after {
    display:inline-block;
    position:absolute;
    left:3px;
    top:3px;
    color:grey;
    font-family:fontAwesome;
    content:'\f002';
}

Demo Fiddle (hiding the icon on focus)

If you wish to hide the icon on focus, you simply need to give the input a background color and a z-index with and without focus, the :focus z-index should be high enough to force it to the front to display over the icon.

CSS:

input{
    z-index:0;
    position:relative;
}
.search{
    display:inline-block;  
    position:relative;
}
.search:after{
    display:inline-block;
    position:absolute;
    right:5px;
    top:5px;
    color:grey;
    font-family:fontAwesome;
    content:'\f002';
}
input:focus{
    z-index:99;
}
SW4
  • 69,876
  • 20
  • 132
  • 137
  • provided Demo working fine but in my case i want to hide the icon while perform the editing – Raja Mar 19 '14 at 13:06
1

check this demo jsFiddle

HTML

<input type="text" class="inputtext" placeholder="Search">

CSS

input { 
    font-family: 'FontAwesome';
    background-image: url(http://www.levenmetwater.nl/static/global/images/icon-search.png);
    background-position: 10px center;
    background-repeat: no-repeat;
    text-indent: 30px;
}
input:focus {
    background-position: -20px center;
    text-indent: 0;
}

Without input:focus demo jsFiddle

Jaykumar Patel
  • 26,836
  • 12
  • 74
  • 76