Use CSS to update Input field - css

Is there a way to use CSS to update input fields without changing HTML code?
I have a form like this:
// HTML
<div id="LoginFormContainer">
<div class="formInputLine">
<div class="inputContainer">
<input name="txtUserID$Textbox1" type="text" maxlength="15" id="txtUserID_Textbox1" placeholder="Username" title="Username">
</div>
</div>
<div class="formInputLine">
<div class="inputContainer">
<input name="txtPassword$Textbox1" type="password" maxlength="15" id="txtPassword_Textbox1" placeholder="Password" title="Password">
</div>
</div>
<div class="formInputLine">
<input type="submit" name="btnLogin" value="Login" id="btnLogin"><input name="builderID" type="hidden" id="builderID" value="abc">
</div>
</div>
//CSS
#FormLoginPage #LoginFormContainer .formInputLine .inputContainer input {
text-transform: uppercase!important;
}
#FormLoginPage #LoginFormContainer .formInputLine .inputContainer input {
border: none;
font-size: 12px;
width: 100%;
color: #333;
outline: 0;
-webkit-appearance: caret;
}
// TRYING CSS - able to use this code to add a label but it applies to all input. Not sure how to target only the individual class with a specific id within it.
.formInputLine::before {
content: "Username";
}
And would like to change it to the following using only CSS:
Please note that the above code is actually part of this code I got from a 3rd party. So I am not sure if I can control it via the iframe tag.
Thanks for the help, I greatly appreciate it.

If the input fields have wrapper elements you can use pseudo elements (before or after) on that wrapper to create what you want with pure css, otherwise you'll have to use javascript to manipulate the html structure / add elements etc.
So, for an example, if we have the following HTML structure:
<div class="input-wrapper">
<input type="text" placeholder="some text">
</div>
We can do the following in CSS:
.input-wrapper {
position: relative;
}
.input-wrapper:before {
position: absolute;
left: 0;
bottom: calc( 100% + 10px );
content: "some text";
}
::-webkit-input-placeholder {
color: transparent !important;
}
(This one is used if we have a placeholder and we want to hide it. On production should also use the -moz- and -ms- prefixes).

You could have something like this:
I've included my own font, due to lack of context.
body {font-family: "Droid Sans"}
.Input-Element {padding: .3em;margin: .5em 0}
.Input-Element label {display: block;text-transform: uppercase;padding: .2em 0;font-size: .8em}
.Input-Element input {border:1px solid rgba(0, 0, 0, 0.34);padding:.5em;outline:none;transition: border .25s}
.Input-Element input:focus {border-color: rgba(0, 0, 0, 0.73)}
<link href="https://fonts.googleapis.com/css?family=Droid+Sans" rel="stylesheet">
<div class='Input-Element'>
<label>Username</label>
<input name='user'>
</div>
<div class='Input-Element'>
<label>Password</label>
<input name='psw'>
</div>
Note: Click Run Code Snippet to see the form!

I was playing with the ideas provided by a few solutions here. After some researching on my own with :nth-child, here is the solution I have for my question. I am sure there is an other way to do the CSS selection. But this is what I have for now.
Using the CSS below can target the two fields individually and add the specific labels
/* add labels */
.formInputLine:nth-child(1)::before {
content: "Username";
}
.formInputLine:nth-child(2)::before {
content: "Password";
}
/* remove place holder */
::-webkit-input-placeholder {
color: transparent;
}
:-moz-placeholder {
/* Firefox 18- */
color: transparent;
}
::-moz-placeholder {
/* Firefox 19+ */
color: transparent;
}
:-ms-input-placeholder {
color: transparent;
}

You can use some jquery and css
$("input").wrap("<div class='custom-input'></div>");
$('.custom-input').eq(0).before("<label>USER NAME</label>");
$('.custom-input').eq(1).before("<label>PASSWORD</label>");
::-webkit-input-placeholder, ::-moz-placeholder, :-ms-input-placeholder {
color: transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" valu "USER NAME" placeholder="USER NAME"><br>
<input type="passsword" placeholder="PASSWORD">

Related

MaterializeCSS autocomplete edit css

I have been trying to customize a search bar that I initially created using MaterializeCSS. I am using the Autocomplete functionality in order to make searching easier. However, I have been unable to successfully change the green MaterializeCSS font colors and size, etc. Both in the input and in the dropdown that appears when people search. I would very much appreciate if someone here could help. I have tried changing the CSS in lots of ways but have been unsuccessful.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css">
<section id="search" class="section white-text blue section-search center scrollspy">
<div class="container">
<div class="row">
<div class="col s12">
<h3>Search destinations</h3>
<div class="input-field">
<i class="material-icons prefix">search</i>
<input type="text" class="white grey-text autocomplete" id="autocomplete-input" placeholder="Search" />
</div>
</div>
</div>
</div>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
CSS:
.section-search input {
padding: 5px !important;
font-size: 18px !important;
width: 90% !important;
border: #f4f4f4 3px solid !important;
background-color: red !important;
}
.section-search input .autocomplete {
color: #000 !important;
}
Javascript:
// Autocomplete
const ac = document.querySelector('.autocomplete');
M.Autocomplete.init(ac, {
data: {
"Banana": null,
"Apple": null,
"Coconut": null,
"Carrot": null,
"Pear": null,
}
});
It looks like your input element has a class of .grey-text that has a color: #9e9e9e !important; declaration which will override any color styles you add. I'd recommend removing that class (and maybe the .white class):
<input type="text" class="autocomplete" id="autocomplete-input" placeholder="Search" />
And your CSS has an extra space between input and .autocomplete (also don't need the !important anymore):
.section-search input.autocomplete {
color: #000;
}
.input-field .prefix.active {
color: #000!important;
}
.dropdown-content li>a, .dropdown-content li>span {
color: #000!important;
}
.autocomplete-content li .highlight {
color: red!important;
}
Working Example

Remove form label when input is active/focus

Trying to remove a icon when the input is active/focused. The problem is that :after do not work. When I click on the input the label is still visible.
Tried with content: none; but that didn't work.
#search-label {
position: relative;
font-weight: normal;
}
#search-label:before {
content:"\f002";
font-family: FontAwesome;
font-size: 115%;
position: absolute;
color: #dddddd;
top: 5px;
left: 8px;
}
Edit:
<div class="form-group">
<label id="search-label">
<input id="search-form" class="form-control" placeholder="{% trans "Search" %}" type="text" name="q" value="{{ request.REQUEST.q }}">
</label>
</div>
Can I do this with CSS or do I need JavaScript for this?
Image of it: http://imgur.com/a/pA1uN
It really depends on your markup, and if you have the freedom and/or ability to modify it.
Yes, the markup can be changed: CSS-only solution
If your label is a sibling and occurs after the input, you can toggle its display status based on the state of the input element itself, for example:
.input-row {
position: relative;
}
.input-row label {
position: absolute;
left: 0;
}
.input-row label::before {
transition: 1s;
/* Just to show how the toggle works */
}
.input-row label.icon1::before {
content: 'icon1';
}
.input-row label.icon2::before {
content: 'icon2';
}
.input-row input {
padding: 0 2rem 0;
}
.input-row input:focus + label::before,
.input-row input:active + label::before {
opacity: 0; /* To visually hide the label's pseudoelement */
pointer-events: none; /* To prevent any cursor-based interaction */
}
<form>
<div class="input-row">
<input type="text" name="input1" id="input1" />
<label for="input1" class="icon1"></label>
</div>
<div class="input-row">
<input type="text" name="input2" id="input2" />
<label for="input1" class="icon2"></label>
</div>
</form>
No, the markup cannot be changed: JS-only solution
If the markup cannot be changed, you will be forced to use a JS-only solution. This is because CSS does not have the ability to traverse backwards (i.e. select previous siblings) or up the parent node (i.e. select the wrapping elements). Remember that as pseudo-elements are not part of the DOM, you cannot select them via JS. Instead, you will have to toggle a class in order to perform the hiding/showing.
In my proof-of-concept example below using the markup that you have provided after updating your question, you can see how jQuery can be used to achieve the desired function. You can of course rewrite the JS function in native JS ;)
$(function() {
$('.form-group :input')
.focus(function() {
$(this).closest('label').addClass('hide');
})
.blur(function() {
$(this).closest('label').removeClass('hide');
});
});
label {
position: relative;
font-weight: normal;
}
label::before {
content: "icon";
font-family: FontAwesome;
font-size: 115%;
position: absolute;
top: 5px;
left: 8px;
transition: 1s; /* Added just to show the effect of toggling opacity */
}
label.hide::before {
opacity: 0; /* To visually hide the label's pseudoelement */
pointer-events: none; /* To prevent any cursor-based interaction */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label id="search-label">
<input id="search-form" class="form-control" placeholder="Search" type="text" name="q" value="">
</label>
</div>
Maybe someone will find this useful:
$('.form-group')
.focus(function() {
$(this).prev('label').addClass('u-hidden');
})
.blur(function() {
n=$(this).val();
if (n.length<1){
$(this).prev('label').removeClass('u-hidden');}else{
$(this).prev('label').addClass('u-hidden');
}
});
A jQuery solution would look something like this:
$("input").focus(function(){
$("span#your_icon").hide();
}).blur(function(){
$("span#your_icon").show();
});
A JS solution would look like this:
document.getElementById("myInput").addEventListener("focus", function(){
document.getElementById("#your_icon").style.display = 'none';
});
document.getElementById("myInput").addEventListener("blur", function(){
document.getElementById("#your_icon").style.display = 'inline';
});
Instead of label you can use a background image and do somthing like this
.input-search {
background:url('http://www.aljanaedu.com/Limitless/images/icons/14x14/search.png') no-repeat left 10px center;
}
.input-search:focus {
background-image:none;
}
Working Fiddle here
albeit a few years late to the question... but I found the best article that worked wonders for me, using jQuery, this article shows us how to essentially add a class to the label's parent container (div) if the input field is either selected/focused AND if the field has a value, OR remove said class if the input field is NOT focused AND empty. https://knowbility.org/blog/2019/animating-form-labels
$("form input").on("blur input focus", function() {
var $field = $(this).closest(".field");
if (this.value) {
$field.addClass("filled");
} else {
$field.removeClass("filled");
}
});
$("form input").on("focus", function() {
var $field = $(this).closest(".field");
if (this) {
$field.addClass("filled");
} else {
$field.removeClass("filled");
}
});

How can I change the size of a Bootstrap checkbox?

Wondering if its possible to change the size of checkbox as it's possible with buttons. I want it to be bigger, so it makes it easy to press. Right now its looking like this:
Code:
<div class="form-group">
<label class="col-md-7 control-label">Kalsiumklorid: </label>
<div class="col-md-5" >
{{ Form::checkbox('O_Kals_Klor', 1 , array('class' => 'form-control' )) }}
</div>
</div>
Or you can style it with pixels.
.big-checkbox {width: 30px; height: 30px;}
input[type=checkbox]
{
/* Double-sized Checkboxes */
-ms-transform: scale(2); /* IE */
-moz-transform: scale(2); /* FF */
-webkit-transform: scale(2); /* Safari and Chrome */
-o-transform: scale(2); /* Opera */
padding: 10px;
}
It is possible in css, but not for all the browsers.
The effect on all browsers:
http://www.456bereastreet.com/lab/form_controls/checkboxes/
A possibility is a custom checkbox with javascript:
http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/
Following works in bootstrap 4 and displays well in CSS, mobile and has no issues with label spacing.
CSS
.checkbox-lg .custom-control-label::before,
.checkbox-lg .custom-control-label::after {
top: .8rem;
width: 1.55rem;
height: 1.55rem;
}
.checkbox-lg .custom-control-label {
padding-top: 13px;
padding-left: 6px;
}
.checkbox-xl .custom-control-label::before,
.checkbox-xl .custom-control-label::after {
top: 1.2rem;
width: 1.85rem;
height: 1.85rem;
}
.checkbox-xl .custom-control-label {
padding-top: 23px;
padding-left: 10px;
}
HTML
<div class="custom-control custom-checkbox checkbox-lg">
<input type="checkbox" class="custom-control-input" id="checkbox-3">
<label class="custom-control-label" for="checkbox-3">Large checkbox</label>
</div>
You can also make it extra large by declaring checkbox-xl
If anyone from BS team is reading this, it would be really good if you make this available right out of the box, I don't see anything for it in BS 5 either
source
It is possible to implement custom bootstrap checkbox for the most popular browsers nowadays.
You can check my Bootstrap-Checkbox project in GitHub, which contains simple .less file.
There is a good article in MDN describing some techniques, where the two major are:
Label redirects a click event.
Label can redirect a click event to its target if it has the for attribute like in <label for="target_id">Text</label> <input id="target_id" type="checkbox" />, or if it contains input as in Bootstrap case: <label><input type="checkbox" />Text</label>.
It means that it is possible to place a label in one corner of the browser, click on it, and then the label will redirect click event to the checkbox located in other corner producing check/uncheck action for the checkbox.
We can hide original checkbox visually, but make it is still working and taking click event from the label. In the label itself we can emulate checkbox with a tag or pseudo-element :before :after.
General non supported tag for old browsers
Some old browsers does not support several CSS features like selecting siblings p+p or specific search input[type=checkbox]. According to the MDN article browsers that support these features also support :root CSS selector, while others not. The :root selector just selects the root element of a document, which is html in a HTML page. Thus it is possible to use :root for a fallback to old browsers and original checkboxes.
Final code snippet:
:root {
/* larger checkbox */
}
:root label.checkbox-bootstrap input[type=checkbox] {
/* hide original check box */
opacity: 0;
position: absolute;
/* find the nearest span with checkbox-placeholder class and draw custom checkbox */
/* draw checkmark before the span placeholder when original hidden input is checked */
/* disabled checkbox style */
/* disabled and checked checkbox style */
/* when the checkbox is focused with tab key show dots arround */
}
:root label.checkbox-bootstrap input[type=checkbox] + span.checkbox-placeholder {
width: 14px;
height: 14px;
border: 1px solid;
border-radius: 3px;
/*checkbox border color*/
border-color: #737373;
display: inline-block;
cursor: pointer;
margin: 0 7px 0 -20px;
vertical-align: middle;
text-align: center;
}
:root label.checkbox-bootstrap input[type=checkbox]:checked + span.checkbox-placeholder {
background: #0ccce4;
}
:root label.checkbox-bootstrap input[type=checkbox]:checked + span.checkbox-placeholder:before {
display: inline-block;
position: relative;
vertical-align: text-top;
width: 5px;
height: 9px;
/*checkmark arrow color*/
border: solid white;
border-width: 0 2px 2px 0;
/*can be done with post css autoprefixer*/
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-ms-transform: rotate(45deg);
-o-transform: rotate(45deg);
transform: rotate(45deg);
content: "";
}
:root label.checkbox-bootstrap input[type=checkbox]:disabled + span.checkbox-placeholder {
background: #ececec;
border-color: #c3c2c2;
}
:root label.checkbox-bootstrap input[type=checkbox]:checked:disabled + span.checkbox-placeholder {
background: #d6d6d6;
border-color: #bdbdbd;
}
:root label.checkbox-bootstrap input[type=checkbox]:focus:not(:hover) + span.checkbox-placeholder {
outline: 1px dotted black;
}
:root label.checkbox-bootstrap.checkbox-lg input[type=checkbox] + span.checkbox-placeholder {
width: 26px;
height: 26px;
border: 2px solid;
border-radius: 5px;
/*checkbox border color*/
border-color: #737373;
}
:root label.checkbox-bootstrap.checkbox-lg input[type=checkbox]:checked + span.checkbox-placeholder:before {
width: 9px;
height: 15px;
/*checkmark arrow color*/
border: solid white;
border-width: 0 3px 3px 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<p>
Original checkboxes:
</p>
<div class="checkbox">
<label class="checkbox-bootstrap">
<input type="checkbox">
<span class="checkbox-placeholder"></span>
Original checkbox
</label>
</div>
<div class="checkbox">
<label class="checkbox-bootstrap">
<input type="checkbox" disabled>
<span class="checkbox-placeholder"></span>
Original checkbox disabled
</label>
</div>
<div class="checkbox">
<label class="checkbox-bootstrap">
<input type="checkbox" checked>
<span class="checkbox-placeholder"></span>
Original checkbox checked
</label>
</div>
<div class="checkbox">
<label class="checkbox-bootstrap">
<input type="checkbox" checked disabled>
<span class="checkbox-placeholder"></span>
Original checkbox checked and disabled
</label>
</div>
<div class="checkbox">
<label class="checkbox-bootstrap checkbox-lg">
<input type="checkbox">
<span class="checkbox-placeholder"></span>
Large checkbox unchecked
</label>
</div>
<br/>
<p>
Inline checkboxes:
</p>
<label class="checkbox-inline checkbox-bootstrap">
<input type="checkbox">
<span class="checkbox-placeholder"></span>
Inline
</label>
<label class="checkbox-inline checkbox-bootstrap">
<input type="checkbox" disabled>
<span class="checkbox-placeholder"></span>
Inline disabled
</label>
<label class="checkbox-inline checkbox-bootstrap">
<input type="checkbox" checked disabled>
<span class="checkbox-placeholder"></span>
Inline checked and disabled
</label>
<label class="checkbox-inline checkbox-bootstrap checkbox-lg">
<input type="checkbox" checked>
<span class="checkbox-placeholder"></span>
Large inline checked
</label>
I used just "save in zoom", in example:
.my_checkbox {
width:5vw;
height:5vh;
}
I have used this library with sucess
http://plugins.krajee.com/checkbox-x
It requires jQuery and bootstrap 3.x
Download the zip here: https://github.com/kartik-v/bootstrap-checkbox-x/zipball/master
Put the contents of the zip in a folder within your project
Pop the needed libs in your header
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="path/to/css/checkbox-x.min.css" media="all" rel="stylesheet" type="text/css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script src="path/to/js/checkbox-x.min.js" type="text/javascript"></script>
Add the data controls to the element using the data-size="xl" to change the size as shown here http://plugins.krajee.com/cbx-sizes-demo
<label for="element_id">CheckME</label>
<input type="checkbox" name="my_element" id="element_id" value="1" data-toggle="checkbox-x" data-three-state="false" data-size="xl"/>
There are numerous other features as well if you browse the plugin site.
<div id="rr-element">
<label for="rr-1">
<input type="checkbox" value="1" id="rr-1" name="rr[]">
Value 1
</label>
</div>
//do this on the css
div label input { margin-right:100px; }
just use simple css
.big-checkbox {width: 1.5rem; height: 1.5rem;top:0.5rem}
Aqui lo que me ayudo a solucionarlo:
.checkbox-xl .form-check-input
{
scale: 2.5;
}
.checkbox-xl .form-check-label
{
padding-left: 25px;
}
<div class="form-check checkbox-xl">
<input class="form-check-input" type="checkbox" value="1" id="checkbox-3" name="check1"/>
<label class="form-check-label" for="checkbox-3">Etiqueta</label>
</div>

Add Bootstrap Glyphicon to Input Box

How can I add a glyphicon to a text type input box? For example I want to have 'icon-user' in a username input, something like this:
Without Bootstrap:
We'll get to Bootstrap in a second, but here's the fundamental CSS concepts in play in order to do this yourself. As beard of prey points out, you can do this with CSS by absolutely positioning the icon inside of the input element. Then add padding to either side so the text doesn't overlap with the icon.
So for the following HTML:
<div class="inner-addon left-addon">
<i class="glyphicon glyphicon-user"></i>
<input type="text" class="form-control" />
</div>
You can use the following CSS to left and right align glyphs:
/* enable absolute positioning */
.inner-addon {
position: relative;
}
/* style icon */
.inner-addon .glyphicon {
position: absolute;
padding: 10px;
pointer-events: none;
}
/* align icon */
.left-addon .glyphicon { left: 0px;}
.right-addon .glyphicon { right: 0px;}
/* add padding */
.left-addon input { padding-left: 30px; }
.right-addon input { padding-right: 30px; }
Demo in Plunker
Note: This presumes you're using glyphicons, but works equally well with font-awesome.
For FA, just replace .glyphicon with .fa
With Bootstrap:
As buffer points out, this can be accomplished natively within Bootstrap by using Validation States with Optional Icons. This is done by giving the .form-group element the class of .has-feedback and the icon the class of .form-control-feedback.
The simplest example would be something like this:
<div class="form-group has-feedback">
<label class="control-label">Username</label>
<input type="text" class="form-control" placeholder="Username" />
<i class="glyphicon glyphicon-user form-control-feedback"></i>
</div>
Pros:
Includes support for different form types (Basic, Horizontal, Inline)
Includes support for different control sizes (Default, Small, Large)
Cons:
Doesn't include support for left aligning icons
To overcome the cons, I put together this pull-request with changes to support left aligned icons. As it is a relatively large change, it has been put off until a future release, but if you need these features today, here's a simple implementation guide:
Just include the these form changes in css (also inlined via hidden stack snippet at the bottom)*LESS: alternatively, if you are building via less, here's the form changes in less
Then, all you have to do is include the class .has-feedback-left on any group that has the class .has-feedback in order to left align the icon.
Since there are a lot of possible html configurations over different form types, different control sizes, different icon sets, and different label visibilities, I created a test page that shows the correct set of HTML for each permutation along with a live demo.
Here's a demo in Plunker
P.S. frizi's suggestion of adding pointer-events: none; has been added to bootstrap
Didn't find what you were looking for? Try these similar questions:
Add Twitter Bootstrap icon to Input box
Put search icon near textbox bootstrap
Addition CSS for Left Aligned feedback icons
.has-feedback .form-control {
padding-right: 34px;
}
.has-feedback .form-control.input-sm,
.has-feedback.form-group-sm .form-control {
padding-right: 30px;
}
.has-feedback .form-control.input-lg,
.has-feedback.form-group-lg .form-control {
padding-right: 46px;
}
.has-feedback-left .form-control {
padding-right: 12px;
padding-left: 34px;
}
.has-feedback-left .form-control.input-sm,
.has-feedback-left.form-group-sm .form-control {
padding-left: 30px;
}
.has-feedback-left .form-control.input-lg,
.has-feedback-left.form-group-lg .form-control {
padding-left: 46px;
}
.has-feedback-left .form-control-feedback {
left: 0;
}
.form-control-feedback {
line-height: 34px !important;
}
.input-sm + .form-control-feedback,
.form-horizontal .form-group-sm .form-control-feedback {
width: 30px;
height: 30px;
line-height: 30px !important;
}
.input-lg + .form-control-feedback,
.form-horizontal .form-group-lg .form-control-feedback {
width: 46px;
height: 46px;
line-height: 46px !important;
}
.has-feedback label.sr-only ~ .form-control-feedback,
.has-feedback label.sr-only ~ div .form-control-feedback {
top: 0;
}
#media (min-width: 768px) {
.form-inline .inline-feedback {
position: relative;
display: inline-block;
}
.form-inline .has-feedback .form-control-feedback {
top: 0;
}
}
.form-horizontal .has-feedback-left .form-control-feedback {
left: 15px;
}
The official method. No custom CSS required :
<form class="form-inline" role="form">
<div class="form-group has-success has-feedback">
<label class="control-label" for="inputSuccess4"></label>
<input type="text" class="form-control" id="inputSuccess4">
<span class="glyphicon glyphicon-user form-control-feedback"></span>
</div>
</form>
DEMO : http://jsfiddle.net/yajf3b7q
This demo is based on an example in Bootstrap docs. Scroll down to "With Optional Icons" here http://getbootstrap.com/css/#forms-control-validation
Here's a CSS-only alternative. I set this up for a search field to get an effect similar to Firefox (& a hundred other apps.)
Here's a fiddle.
HTML
<div class="col-md-4">
<input class="form-control" type="search" />
<span class="glyphicon glyphicon-search"></span>
</div>
CSS
.form-control {
padding-right: 30px;
}
.form-control + .glyphicon {
position: absolute;
right: 0;
padding: 8px 27px;
}
It can be done using classes from the official bootstrap 3.x version, without any custom css.
Use input-group-addon before the input tag, inside of input-group then use any of the glyphicons, here is the code
<form>
<div class="form-group">
<div class="col-xs-5">
<div class="input-group">
<span class="input-group-addon transparent"><span class="glyphicon glyphicon-user"></span></span>
<input class="form-control left-border-none" placeholder="User Name" type="text" name="username">
</div>
</div>
</div>
</form>
Here is the output
To customise it further add a couple of lines of custom css to your own custom.css file (adjust padding if needed)
.transparent {
background-color: transparent !important;
box-shadow: inset 0px 1px 0 rgba(0,0,0,.075);
}
.left-border-none {
border-left:none !important;
box-shadow: inset 0px 1px 0 rgba(0,0,0,.075);
}
By making the background of the input-group-addon transparent and making the left gradient of the input tag to zero the input will have a seamless appearance. Here is the customised output
Here is a jsbin example
This will solve the custom css problems of overlapping with labels, alignment while using input-lg and focus on tab issue.
Here is how I did it using only the default bootstrap CSS v3.3.1:
<div class="form-group">
<label class="control-label">Start:</label>
<div class="input-group">
<input type="text" class="form-control" aria-describedby="start-date">
<span class="input-group-addon" id="start-date"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
</div>
And this is how it looks:
If you are using Fontawesome you can do this :
<input type="text" style="font-family:Arial, FontAwesome" placeholder="" />
Result
The complete list of unicode can be found in the The complete Font Awesome 4.6.3 icon reference
This 'cheat' will work with the side effect that the glyphicon class will change the font for the input control.
Fiddle
<input class="form-control glyphicon" type="search" placeholder=""/>
If you want to get rid of the side effect you can remove the "glyphicon" class and add the following CSS (There may be a better way to style the placeholder pseudo element and I've only tested on Chrome).
Fiddle
.form-control[type="search"]::-webkit-input-placeholder:first-letter {
font-family:"Glyphicons Halflings";
}
.form-control[type="search"]:-moz-placeholder:first-letter {
font-family:"Glyphicons Halflings";
}
.form-control[type="search"]::-moz-placeholder:first-letter {
font-family:"Glyphicons Halflings";
}
.form-control[type="search"]:-ms-input-placeholder:first-letter {
font-family:"Glyphicons Halflings";
}
Possibly an even cleaner solution:
Fiddle
CSS
.form-control.glyphicon {
font-family:inherit;
}
.form-control.glyphicon::-webkit-input-placeholder:first-letter {
font-family:"Glyphicons Halflings";
}
.form-control.glyphicon:-moz-placeholder:first-letter {
font-family:"Glyphicons Halflings";
}
.form-control.glyphicon::-moz-placeholder:first-letter {
font-family:"Glyphicons Halflings";
}
.form-control.glyphicon:-ms-input-placeholder:first-letter {
font-family:"Glyphicons Halflings";
}
HTML
<input class="form-control glyphicon" type="search" placeholder=" search" />
<input class="form-control glyphicon" type="text" placeholder=" username" />
<input class="form-control glyphicon" type="password" placeholder=" password" />
Here is a non-bootstrap solution that keeps your markup simple by embedding the image representation of the glyphicon directly in the CSS using base64 URI encoding.
input {
border:solid 1px #ddd;
}
input.search {
padding-left:20px;
background-repeat: no-repeat;
background-position-y: 1px;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADbSURBVDhP5ZI9C4MwEIb7//+BEDgICA6C4OQgBJy6dRIEB6EgCNkEJ4e3iT2oHzH9wHbpAwfyJvfkJDnhYH4kHDVKlSAigSAQoCiBKjVGXvaxFXZnxBQYkSlBICII+22K4jM63rbHSthCSdsskVX9Y6KxR5XJSSpVy6GbpbBKp6aw0BzM0ShCe1iKihMXC6EuQtMQwukzPFu3fFd4+C+/cimUNxy6WQkNnmdzL3NYPfDmLVuhZf2wZYz80qDkKX1St3CXAfVMqq4cz3hTaGEpmctxDPmB0M/fCYEbAwZYyVKYcroAAAAASUVORK5CYII=);
}
<input class="search">
input {
border:solid 1px #ddd;
}
input.search {
padding-left:20px;
background-repeat: no-repeat;
background-position-y: 1px;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADbSURBVDhP5ZI9C4MwEIb7//+BEDgICA6C4OQgBJy6dRIEB6EgCNkEJ4e3iT2oHzH9wHbpAwfyJvfkJDnhYH4kHDVKlSAigSAQoCiBKjVGXvaxFXZnxBQYkSlBICII+22K4jM63rbHSthCSdsskVX9Y6KxR5XJSSpVy6GbpbBKp6aw0BzM0ShCe1iKihMXC6EuQtMQwukzPFu3fFd4+C+/cimUNxy6WQkNnmdzL3NYPfDmLVuhZf2wZYz80qDkKX1St3CXAfVMqq4cz3hTaGEpmctxDPmB0M/fCYEbAwZYyVKYcroAAAAASUVORK5CYII=);
}
<input class="search">
Here's another way to do it by placing the glyphicon using the :before pseudo element in CSS.
Working demo in jsFiddle
For this HTML:
<form class="form form-horizontal col-xs-12">
<div class="form-group">
<div class="col-xs-7">
<span class="usericon">
<input class="form-control" id="name" placeholder="Username" />
</span>
</div>
</div>
</form>
Use this CSS (Bootstrap 3.x and Webkit-based browsers compatible)
.usericon input {
padding-left:25px;
}
.usericon:before {
height: 100%;
width: 25px;
display: -webkit-box;
-webkit-box-pack: center;
-webkit-box-align: center;
position: absolute;
content: "\e008";
font-family: 'Glyphicons Halflings';
pointer-events: none;
}
As #Frizi said, we have to add pointer-events: none; so that the cursor doesn't interfere with the input focus. All the others CSS rules are for centering and adding the proper spacing.
The result:
You can use its Unicode HTML
So to add a user icon, just add  to the placeholder attribute, or wherever you want it.
You may want to check this cheat sheet.
Example:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<input type="text" class="form-control" placeholder=" placeholder..." style="font-family: 'Glyphicons Halflings', Arial">
<input type="text" class="form-control" value=" value..." style="font-family: 'Glyphicons Halflings', Arial">
<input type="submit" class="btn btn-primary" value=" submit-button" style="font-family: 'Glyphicons Halflings', Arial">
Don't forget to set the input's font to the Glyphicon one, using the
following code: font-family: 'Glyphicons Halflings', Arial, where
Arial is the font of the regular text in the input.
Tested with Bootstrap 4.
Take a form-control, and add is-valid to its class. Notice how the control turns green, but more importantly, notice the checkmark icon on the right of the control? This is what we want!
Example:
.my-icon {
padding-right: calc(1.5em + .75rem);
background-image: url('https://use.fontawesome.com/releases/v5.8.2/svgs/regular/calendar-alt.svg');
background-repeat: no-repeat;
background-position: center right calc(.375em + .1875rem);
background-size: calc(.75em + .375rem) calc(.75em + .375rem);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="container">
<div class="col-md-5">
<input type="text" id="date" class="form-control my-icon" placeholder="Select...">
</div>
</div>
I also have one decision for this case with Bootstrap 3.3.5:
<div class="col-sm-5">
<label for="date">
<input type="date" placeholder="Date" id="date" class="form-control">
</label>
<i class="glyphicon glyphicon-calendar col-sm-pull-2"></i>
</div>
On input I have something like this:
Here's how it works in pure Bootstrap 5:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>iconbutton</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" rel="stylesheet">
</head>
<body>
<div class="d-flex flex-row align-items-center m-4 border rounded">
<i class="fa-solid fa-search mx-2"></i>
<input type="text" class="form-control border-0" placeholder="Search">
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
To remove the border that appears on focus, add shadow-0 to the input.
If you are fortunate enough to only need modern browsers: try css transform translate. This requires no wrappers, and can be customized so that you can allow more spacing for input[type=number] to accomodate the input spinner, or move it to the left of the handle.
#import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css");
.is-invalid {
height: 30px;
box-sizing: border-box;
}
.is-invalid-x {
font-size:27px;
vertical-align:middle;
color: red;
top: initial;
transform: translateX(-100%);
}
<h1>Tasty Field Validation Icons using only css transform</h1>
<label>I am just a poor boy nobody loves me</label>
<input class="is-invalid"><span class="glyphicon glyphicon-exclamation-sign is-invalid-x"></span>
http://codepen.io/anon/pen/RPRmNq?editors=110
You should be able to do this with existing bootstrap classes and a little custom styling.
<form>
<div class="input-prepend">
<span class="add-on">
<i class="icon-user"></i>
</span>
<input class="span2" id="prependedInput" type="text" placeholder="Username" style="background-color: #eeeeee;border-left: #eeeeee;">
</div>
Edit The icon is referenced via the icon-user class. This answer was written at the time of Bootstrap version 2. You can see the reference on the following page: http://getbootstrap.com/2.3.2/base-css.html#images

Display image outside html element with CSS

is it possible to display an image outside of textbox with CSS only?
Please see my example below:
Of course, with CSS2 it is completely possible (although it is not supported by IE):
input.test:before
{
padding: 4px;
content: url(images/your_image.gif);
}
For more info, check out the following links:
http://www.quirksmode.org/css/beforeafter.html
http://www.w3.org/TR/CSS2/generate.html
If you want something that works in IE as well, you will have to do it with javascript, I'm afraid.
Why not make use of the label element? i.e:
<style>
.required { padding-right: 20px; background: url(...) no-repeat 50% 100%; }
</style>
<label class="required" title="This is a required field"><dfn>My Field:</dfn> <input type="text" /></label>
You could use a container:
CSS
#field-container
{
padding-right: 10px;
background: transparent url(images/dot.gif) middle right no-repeat;
}
HTML
<div id="field-container" class="form-row">
<label for="field">Field:</label>
<input type="text" value="" id="field" name="field" />
</div>

Resources