How do I put an icon inside a form's input element?
https://i.stack.imgur.com/V0Tjp.png
Live version at: Tidal Force theme
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;
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.
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; }
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; }
You can try this:
input[type='text'] {
background-image: url(images/comment-author.gif);
background-position: 7px 7px;
background-repeat: no-repeat;
}
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; }
A simple and easy way to position an Icon inside of an input is to use the position CSS property as shown in the code below. Note: I have simplified the code for clarity purposes.
Create the container surrounding the input and icon. Set the container position as relative Set the icon as position absolute. This will position the icon relative to the surrounding container. Use either top, left, bottom, right to position the icon in the container. Set the padding inside the input so the text does not overlap the icon.
#input-container { position: relative; } #input-container > img { position: absolute; top: 12px; left: 15px; } #input-container > input { padding-left: 40px; }
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.
.icon{
background: url(1.jpg) no-repeat;
padding-left:25px;
}
add above tags into your CSS file and use the specified class.
This works for me:
input.valid { border-color: #28a745; padding-right: 30px; background-image: url('https://www.stephenwadechryslerdodgejeep.com/wp-content/plugins/pm-motors-plugin/modules/vehicle_save/images/check.png'); background-repeat: no-repeat; background-size: 20px 20px; background-position: right center; }
You Can Try this : Bootstrap-4 Beta
https://www.codeply.com/go/W25zyByhec
<div class="container">
<form>
<div class="row">
<div class="input-group mb-3 col-sm-6">
<input type="text" class="form-control border-right-0" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
<div class="input-group-prepend bg-white">
<span class="input-group-text border-left-0 rounded-right bg-white" id="basic-addon1"><i class="fas fa-search"></i></span>
</div>
</div>
</div>
</form>
</div>
use this css class for your input at start, then customize accordingly:
.inp-icon { background: url(https://i.imgur.com/kSROoEB.png)no-repeat 100%; background-size: 16px; }
I achieved this with the code below.
First, you flex the container which makes the input and the icon be on the same line. Aligning items makes them be on the same level.
Then, make the input take up 100% of the width regardless. Give the icon absolute positioning which allows it to overlap with the input.
Then add right padding to the input so the text typed in doesn't get to the icon. And finally use the right
css property to give the icon some space from the edge of the input.
Note: The Icon
tag could be a real icon if you are working with ReactJs or a placeholder for any other way you work with icons in your project.
.inputContainer { display: flex; align-items: center; position: relative; } .input { width: 100%; padding-right: 40px; } .inputIcon { position: absolute; right: 10px; }
Just use the background property in your CSS.
<input id="foo" type="text" />
#foo
{
background: url(/img/foo.png);
}
I had situation like this. It didn't work because of background: #ebebeb;
. I wanted to put background on the input field and that property was constantly showing up on the top of the background image, and i couldn't see the image! So, I moved the background
property to be above the background-image
property and it worked.
input[type='text'] {
border: 0;
background-image: url('../img/search.png');
background-position: 9px 20px;
background-repeat: no-repeat;
text-align: center;
padding: 14px;
background: #ebebeb;
}
Solution for my case was:
input[type='text'] {
border: 0;
background: #ebebeb;
background-image: url('../img/search.png');
background-position: 9px 20px;
background-repeat: no-repeat;
text-align: center;
padding: 14px;
}
Just to mention, border
, padding
and text-align
properties are not important for the solution. I just replicated my original code.
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
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).
Using with font-icon
<input name="foo" type="text" placeholder="">
OR
<input id="foo" type="text" />
#foo::before
{
font-family: 'FontAwesome';
color:red;
position: relative;
left: -5px;
content: "\f007";
}
div
.
<label for="fileEdit">
<i class="fa fa-cloud-upload">
</i>
<input id="fileEdit" class="hidden" type="file" name="addImg" ng-file-change="onImageChange( $files )" ng-multiple="false" accept="{{ contentType }}"/>
</label>
For example you can use this : label with hidden input (icon is present).
I was able to add an icon to an input field by adding the icon as a background image through CSS. From there, you can adjust the size of the image using the background-size property and finally, position the element with the background-position-x and background-position-y properties. I've shared a code snippet below and linked to a working example in Codepen here:
body { margin: 0; padding: 0; font-family: sans-serif; } .input-container { padding: 50px; } input { box-sizing: border-box; width: 250px; padding-left: 36px; height: 48px; background-image: url('https://image.shutterstock.com/image-vector/apple-icon-vector-fruit-symbol-260nw-1466147615.jpg'); background-size: 20px; background-position-x: 10px; background-position-y: 50%; background-repeat: no-repeat; border-radius: 15px; }
https://codepen.io/Iram_Tech/pen/GRQqrNg
I didn't want to change the background of my input text neither it will work with my SVG icon.
What i did is adding negative margin to the icon so it appear inside the input box
and adding same value padding to the input so text won't go under the icon.
<div class="search-input-container">
<input
type="text"
class="search-input"
style="padding-right : 30px;"
/>
<img
src="@/assets/search-icon.svg"
style="margin-left: -30px;"
/>
</div>
*inline-style is for readability consider using classes
.input_container { display: flex; border-bottom: solid 1px grey; transition: border-color 0.1s ease-in; background: white; } .input { color: blue; display: block; width: calc(100% - 20px); border: none; outline: none; padding: 8px 16px; } .input_img { flex-basis: 20px; display: inline-block; padding: 8px 16px; cursor: pointer; }
You could go for a different approach which also allows you to click it and have it do a function. Have a look at the example below:
<div id="search-bar">
<input placeholder="Search or Type a URL">
<button><i class="fas fa-search"></i></button>
</div>
#search-bar {
display: flex;
justify-content: center;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
height: 60px;
}
#search-bar > input {
width: 750px;
font-size: 30px;
padding: 20px;
border-radius: 50px 0px 0 50px;
border: none;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
border-left: 1px solid #000;
background: #fff; /* CSS Edit Here */
}
#search-bar > button {
background: #fff;
border: none;
font-size: 30px;
border-right: 1px solid #000;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
border-radius: 0 50px 50px 0 ;
padding-right: 20px;
}
The css background solutions do it for most cases, but it has a problem with webkit (chrome) autocomplete where the icon disappear.
There are other solutions that includes changing the html/dom structure by wrapping the input in a div and adding an extra element (img, div, or similar). I don't like does solutions because you need to tweak the elements css with absolute positions and/or resizing by pixel to get the right place. Or recreate the input border to "merge" input and img in one.
So this solution is based on css background image not applied over input element, but applied over a wrapper div.
HTML:
<div class="input-email">
<input type="text" placeholder="Email" name="email" id="email">
</div>
CSS:
.input-email {
background: url(/assets/images/email.svg) no-repeat scroll 14px 11px;
display: inline-block;
}
.input-email input{
padding-left: 40px;
background-color: transparent !important;
}
input:-webkit-autofill, input:-webkit-autofill:hover,
input:-webkit-autofill:focus, input:-webkit-autofill:active {
transition: background-color 5000s ease-in-out 0s;
}
This way with .input-email class I define my icon image as div background (not affected by webkit autocomplete background). Next .input-email input definition I pad left the input element to give space for the image and set it as transparent (this works when autocomplete is not applied) Finally with webkit-autofill classes I remove with transition the background-color set by the autocomplete.
Note: at point 2 I set transparent !important because this -internal-autofill-selected gets rendered at browser and I couldn't overwrite it without setting my also as !important:
input:-internal-autofill-selected {
background-color: -internal-light-dark(rgb(232, 240, 254), rgba(70, 90, 126, 0.4)) !important;
}
I got my solution from this post https://www.py4u.net/discuss/1069380 I have make some tweaks, though major credits are to them.
This works for me for more or less standard forms:
<button type="submit" value="Submit" name="ButtonType" id="whateveristheId" class="button-class">Submit<img src="/img/selectedImage.png" alt=""></button>
Success story sharing