How can we add a Twitter Bootstrap icon icon-search to the right of a text input element?
The following attempt placed all the icons inside the input element, how can we crop it so it only displays the icon for icon-search?
Current Attempt
CSS
input.search-box {
width: 180px;
background: #333;
background-image: url('/img/glyphicons-halflings-white.png');
background-position: -48px 0;
padding-left: 30px;
margin-top: 45px;
border: 0;
float: right;
}
Updated Bootstrap 3.x
You can use the .input-group class like this:
<div class="input-group">
<input type="text" class="form-control"/>
<span class="input-group-addon">
<i class="fa fa-search"></i>
</span>
</div>
Working Demo in jsFiddle for 3.x
Bootstrap 2.x
You can use the .input-append class like this:
<div class="input-append">
<input class="span2" type="text">
<button type="submit" class="btn">
<i class="icon-search"></i>
</button>
</div>
Working Demo in jsFiddle for 2.x
Both will look like this:
If you'd like the icon inside the input box, like this:
Then see my answer to Add a Bootstrap Glyphicon to Input Box
Bootstrap 5 with 3 different way.
Icon with default bootstrap Style
<div class="input-group">
<input type="text" class="form-control" placeholder="From" aria-label="from" aria-describedby="from">
<span class="input-group-text"><i class="fas fa-map-marker-alt"></i></span>
</div>
Icon Inside Input with default bootstrap class
<div class="input-group">
<input type="text" class="form-control border-end-0" placeholder="From" aria-label="from" aria-describedby="from">
<span class="input-group-text bg-transparent"><i class="fas fa-map-marker-alt"></i></span>
</div>
Icon Inside Input with small custom css
<div class="input-group">
<input type="text" class="form-control rounded-end" placeholder="From" aria-label="from" aria-describedby="from">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span>
</div>
Custom Css
.icon-inside {
position: absolute;
right: 10px;
top: calc(50% - 12px);
pointer-events: none;
font-size: 16px;
font-size: 1.125rem;
color: #c4c3c3;
z-index:3;
}
.icon-inside {
position: absolute;
right: 10px;
top: calc(50% - 12px);
pointer-events: none;
font-size: 16px;
font-size: 1.125rem;
color: #c4c3c3;
z-index:3;
}
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<div class="container">
<h5 class="mt-3">Icon <small>with default bootstrap Style</small><h5>
<div class="input-group">
<input type="text" class="form-control" placeholder="From" aria-label="from" aria-describedby="from">
<span class="input-group-text"><i class="fas fa-map-marker-alt"></i></span>
</div>
<h5 class="mt-3">Icon Inside Input <small>with default bootstrap class</small><h5>
<div class="input-group">
<input type="text" class="form-control border-end-0" placeholder="From" aria-label="from" aria-describedby="from">
<span class="input-group-text bg-transparent"><i class="fas fa-map-marker-alt"></i></span>
</div>
<h5 class="mt-3">Icon Inside Input<small> with small custom css</small><h5>
<div class="input-group">
<input type="text" class="form-control rounded-end" placeholder="From" aria-label="from" aria-describedby="from">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
Bootstrap 4.x with 3 different way.
Icon with default bootstrap Style
<div class="input-group">
<input type="text" class="form-control" placeholder="From" aria-label="from" aria-describedby="from">
<div class="input-group-append">
<span class="input-group-text"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
Icon Inside Input with default bootstrap class
<div class="input-group">
<input type="text" class="form-control border-right-0" placeholder="From" aria-label="from" aria-describedby="from">
<div class="input-group-append">
<span class="input-group-text bg-transparent"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
Icon Inside Input with small custom css
<div class="input-group">
<input type="text" class="form-control rounded-right" placeholder="From" aria-label="from" aria-describedby="from">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span>
</div>
Custom Css
.icon-inside {
position: absolute;
right: 10px;
top: calc(50% - 12px);
pointer-events: none;
font-size: 16px;
font-size: 1.125rem;
color: #c4c3c3;
z-index:3;
}
.icon-inside {
position: absolute;
right: 10px;
top: calc(50% - 12px);
pointer-events: none;
font-size: 16px;
font-size: 1.125rem;
color: #c4c3c3;
z-index:3;
}
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<div class="container">
<h5 class="mt-3">Icon <small>with default bootstrap Style</small><h5>
<div class="input-group">
<input type="text" class="form-control" placeholder="From" aria-label="from" aria-describedby="from">
<div class="input-group-append">
<span class="input-group-text"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
<h5 class="mt-3">Icon Inside Input <small>with default bootstrap class</small><h5>
<div class="input-group">
<input type="text" class="form-control border-right-0" placeholder="From" aria-label="from" aria-describedby="from">
<div class="input-group-append">
<span class="input-group-text bg-transparent"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
<h5 class="mt-3">Icon Inside Input<small> with small custom css</small><h5>
<div class="input-group">
<input type="text" class="form-control rounded-right" placeholder="From" aria-label="from" aria-describedby="from">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span>
</div>
</div>
Since the glyphicons image is a sprite, you really can't do that: fundamentally what you want is to limit the size of the background, but there's no way to specify how big the background is. Either you cut out the icon you want, size it down and use it, or use something like the input field prepend/append option (http://twitter.github.io/bootstrap/base-css.html#forms and then search for prepended inputs).
For bootstrap 4
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<form class="form-inline my-2 my-lg-0">
<div class="input-group">
<input class="form-control" type="search" placeholder="Search">
<div class="input-group-append">
<div class="input-group-text"><i class="fa fa-search"></i></div>
</div>
</div>
</form>
Demo
Bootstrap 4.x
With Bootstrap 4 (and Font Awesome), we still can use the input-group wrapper around our form-control element, and now we can use an input-group-append (or input-group-prepend) wrapper with an input-group-text to get the job done:
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Search" aria-label="Search" aria-describedby="my-search">
<div class="input-group-append">
<span class="input-group-text" id="my-search"><i class="fas fa-filter"></i></span>
</div>
</div>
It will look something like this (thanks to KyleMit for the screenshot):
Learn more by visiting the Input group documentation.
Bootstrap 5
According to Bootstrap 5 docs
.input-group-append and .input-group-prepend. You can now just add
buttons and .input-group-text as direct children of the input groups.
Example:
<div class="input-group">
<div class="input-group-text">File input</div>
<input class="form-control" id="formFile" type="file" />
<button class="btn border" type="button" style="background-color: #e9ecef;">
<i class="fas fa-times"></i>
</button>
</div>
Related
I am trying to add the search icon in a input text, but I don't know how to do it.
I want to do something like:
input search
Is it possible to use Bootstrap Icon in a Placeholder? I saw some examples, but the icon didn't disappear when I was writing text
Add Css
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
Add Js
<script src="https://kit.fontawesome.com/yourcode.js"></script>
Add input in <form>
<input style="font-family: FontAwesome;" placeholder=' Search...' />
You can try using position CSS set position for icon-search
<div class="form-group search">
<i class="fa fa-search"></i>
<input type="text" class="form-control" id="search" placeholder="Search">
</div>
div.search {
margin: 1em;
position: relative;
input {
padding: 1em 1em 1em 3em;
}
i {
position: absolute;
top: 2px;
opacity: 0.6;
padding: 1em;
display: flex;
align-items: center;
}
}
or an input group Search input with an icon Bootstrap
https://www.codeply.com/go/ioPsDfyCBc
You can try this,
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-search" style="color:black"></i></span>
<input type="text" class="form-control" placeholder="Search"/>
</div>
In bootstrap 5 you can simply use a button with position-relative class and transparent background.
<form class="row">
<div class="col-sm-12 col-lg-9">
<input class="form-control ps-5" type="search" placeholder="Search items..." aria-label="Search">
<button style="bottom: 20px;" class="btn bg-transparent px-2 py-0 position-relative translate-middle-y" type="submit">
<!-- You will need to import bootstrap icons or font-awesome -->
<i class="bi bi-search fs-5"></i>
</button>
</div>
<div class="col-sm-12 col-lg-3">
<!-- Here you can add a button width with w-100 class it's optional -->
<a class="btn btn-primary w-100" href="#">Filters Items</a>
</div>
</form>
<div class="card">
<div class="col-md-12">
<span style="margin-left:20px;">
<input rows="4" cols="50" type="text" name="box" id="box" style="width:50px;float:left;"></span>
</span>
<i class="fa fa-long-arrow-left" style="font-size:36px;vertical-align:-19%;color:green"></i>
<span>
<input type="text" name="boxx" id="boxx" style="width:200px;">
</span>
<i class="fa fa-long-arrow-right" style="font-size:48px;color:green"></i>
<span style="margin-right:20px;">
<input rows="4" cols="50" type="text" name="box" id="box" style="width:50px;float:right;">
</span>
</div>
</div>
i want to align the middle textbox in the center of the div and the icon should be close to the textbox.
You set the font size of the second < i > to 48 and caused this error. And I changed the bootstrap classes to a row with 3 cols 3-6-3. make sure that your bootstrap references were referenced well :
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class ="row">
<div class="col-sm-3">
<span>
<input rows="4" cols="50" type="text" name="box" id="box" style="width:50px;float:left;"></span>
</span>
<i class="fa fa-long-arrow-left" style="font-size:36px;color:green"></i>
</div>
<div class="col-sm-6">
<span>
<input type="text" name="boxx" id="boxx" style="width:200px;">
</span>
<i class="fa fa-long-arrow-right" style="color:green"></i>
</div>
<div class="col-sm-3">
<span>
<input rows="4" cols="50" type="text" name="box" id="box" style="width:50px;float:right;">
</span>
</div>
</div>
You can use display: flex to center everything :
.col-md-12 {
display: flex;
align-items: center;
}
#input-left {
width:50px;
}
#input-center {
width:200px;
}
#input-right {
width:50px;
}
.middle-box {
margin-left: auto;
margin-right: auto;
display: flex;
align-items: center;
}
.fa-long-arrow-left {
font-size:36px !important;
/*vertical-align:-19%;*/
color:green;
}
.fa-long-arrow-right {
font-size:48px !important;
color:green;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="card">
<div class="col-md-12">
<span style="margin-left:20px;">
<input rows="4" cols="50" type="text" name="box" id="input-left"> </span>
<span class="middle-box">
<i class="fa fa-long-arrow-left"></i>
<span>
<input type="text" name="boxx" id="input-center">
</span>
<i class="fa fa-long-arrow-right"></i>
</span>
<span style="margin-right:20px;">
<input rows="4" cols="50" type="text" name="box" id="input-right">
</span>
</div>
</div>
Can you help me with this problem? I have an input-group within bootstrap 4. In my input-group I have 2 inputs, first with prepend and second with append.
It's wrapped in a col-12 div in a form-group and I need to set the width of the first input and second to some specific (but different) value. I tried to set the width in my custom css, but without success. Is it possible? I know, that I can set it via wrapping it in a div with another col, but than I have both input below and not side by side.
Code:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="form-group">
<label for="telephone">Telephone</label>
<div class="row">
<div class="col-12">
<div class="input-group telephone-input">
<div class="input-group-prepend preselection-prepend">
<div class="input-group-text">+</div>
</div>
<input class="form-control" id="preselection" type="text" maxlength="3" name="preselection" placeholder="Preselection" autocomplete="off" required>
<input class="form-control" id="telephone" placeholder="Telephone number" type="tel" maxlength="11" name="telephone" autocomplete="off" required>
<span class="placeholder" id="tele-holder"></span>
<div class="input-group-append" id="preselectionToggle">
<div class="input-group-text">
<i class="far fa-plus-square"></i>
</div>
</div>
</div>
</div>
</div>
</div>
<script defer src="https://use.fontawesome.com/releases/v5.0.8/js/all.js" integrity="sha384-SlE991lGASHoBfWbelyBPLsUlwY1GwNDJo3jSJO04KZ33K2bwfV9YBauFfnzvynJ" crossorigin="anonymous"></script>
You can do it with class w-50 and w-100
like this
<div class="input-group">
<div class="input-group-prepend w-50">
<span class="input-group-text w-100">label</span>
</div>
<input type="text" class="form-control " />
</div>
I've found a solution on the internet and you can visit it at codeply!.
It is pretty neat and simple. To do so, you just need to add the following CSS to your stylesheet.
.input-group>.input-group-prepend {
flex: 0 0 30%;
}
.input-group .input-group-text {
width: 100%;
}
To set the width of the input-group-text, just change the value of the flex of the input-group, for instance, as seen above 30% of the total width.
Thank the author of the link above for this simple code snippet.
I found a solution. I tried set max-width instead of only width and it works.
For the above example it would be:
#preselection {
max-width: 15%;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="form-group">
<label for="telephone">Telephone</label>
<div class="row">
<div class="col-12">
<div class="input-group telephone-input">
<div class="input-group-prepend preselection-prepend">
<div class="input-group-text">+</div>
</div>
<input class="form-control" id="preselection" type="text" maxlength="3" name="preselection" placeholder="Preselection" autocomplete="off" required>
<input class="form-control" id="telephone" placeholder="Telephone number" type="tel" maxlength="11" name="telephone" autocomplete="off" required>
<span class="placeholder" id="tele-holder"></span>
<div class="input-group-append" id="preselectionToggle">
<div class="input-group-text">
<i class="far fa-plus-square"></i>
</div>
</div>
</div>
</div>
</div>
</div>
<script defer src="https://use.fontawesome.com/releases/v5.0.8/js/all.js" integrity="sha384-SlE991lGASHoBfWbelyBPLsUlwY1GwNDJo3jSJO04KZ33K2bwfV9YBauFfnzvynJ" crossorigin="anonymous"></script>
How to remove the lines around the icon like place the icon inside the searchbox I tried background transparent border 0 still not working the border are still there
here's what I did
<center>
<div class="input-group col-xs-4 col-md-6" >
<input type="text" name="search" id="search-building" class="form-control" placeholder="Search..." required>
<div class="input-group-addon">
<i class="fas fa-search"></i>
</div>
</div>
</center>
CSS
.input-group-addon{
border:0;
background:white;
pointer-events: none;
}
Your css class rules aren't being applied because of the specificity... to solve it add a parent to your selector..
.input-group .input-group-addon{
border:0;
background:white;
pointer-events: none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="input-group col-xs-4 col-md-6" >
<input type="text" name="search" id="search-building" class="form-control" placeholder="Search..." required>
<div class="input-group-addon">
<i class="fas fa-search"></i>
</div>
Check this out.
UPDATED:
Changed as per the discussion below.
.input-group input {
border-right: none;
}
.input-group .input-group-addon {
background: inherit;
}
div {
background: red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<div>
<center>
<div class="input-group col-xs-4 col-md-6" >
<input type="text" name="search" id="search-building" class="form-control" placeholder="Search..." required>
<div class="input-group-addon">
<i class="fa fa-search"></i>
</div>
</div>
</center></div>
I'm trying to put a Font-awesome icon inside an input tag.
This is my HTML code:
<div class="input-group">
<input class="form-control" type="password" placeholder="Password">
<span class="input-group-addon"><i class="fa fa-key fa-fw"></i></span>
</div>
But that icon is bigger than input box as you can see in this picture:
How can I fix the icon?
Bootstrap 3
Checkout the Bootstrap examples in the Font Awesome documentation:
<div class="input-group margin-bottom-sm">
<span class="input-group-addon"><i class="fa fa-envelope-o fa-fw"></i></span>
<input class="form-control" type="text" placeholder="Email address">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-key fa-fw"></i></span>
<input class="form-control" type="password" placeholder="Password">
</div>
It should work out of the box, so if you still have height differences, check that there is not other CSS stuff that override your input-group-addon height
Working JSFiddle: https://jsfiddle.net/vs0wpy80
Bootstrap 4
Bootstrap 4 introduced some new classes for input groups, we need to use input-group-prepend and input-group-append:
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-envelope-o fa-fw"></i></span>
</div>
<input class="form-control" type="text" placeholder="Email address">
</div>
<div class="input-group mb-3">
<input class="form-control" type="text" placeholder="Email address">
<div class="input-group-append">
<span class="input-group-text"><i class="fa fa-envelope-o fa-fw"></i></span>
</div>
</div>
Working JSFiddle: https://jsfiddle.net/8do9v4dp/5/
input.form-control{
margin-top: 0;
}
In my case it helps
With CSS only
<div class="wrapper">
<input type="text" class="input">
<i class="icon" class="fas fa-some-icon"></i>
</div>
Wrap the input and the icon in a 2 columns grid with this column template: 1fr 0
.wrapper {
display: grid;
grid-template-columns: 1fr 0;
}
Give the icon a relative position from the right:
.icon{
width: 40px;
position: relative;
right: 45px;
font-size: 35px;
line-height: 40px;
cursor: pointer
z-index: 1;
}
Give the input a nice padding on the margin:
.input{
padding-right: 57px;
}
The input will take 100% of the space in the grid and the icon will float atop.
Works well with responsive designs and you can click on the icon.