Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put search icon near textbox using bootstrap

I am using bootstrap by default textbox taking full width of column and I want to put search icon at the end to textbox.

My code is like this:

<div class="container">     <div class="row">         <div class="form-group col-lg-4">             <label class="control-label">Name</label>             <input id="txtName" class="form-control input-sm" />             <span class="glyphicon glyphicon-search"></span>          </div>         <div class="col-lg-4"></div>         <div class="col-lg-4"></div>     </div> </div> 

I don't want to use input group.

Please suggest an alternate way or alternate html with css.

like image 591
Saurabh Mahajan Avatar asked Jul 10 '14 17:07

Saurabh Mahajan


People also ask

How do I add a search icon inside a text box in bootstrap?

Step by step guide for the implementation: Step 1: Include Bootstrap and jQuery CDN into the <head> tag before all other stylesheets to load our CSS. Step 2: Add <div> tag in the HTML body with class container. Step 3: Now add any icon that you want using the below syntax, where the name is the name of glyphicon.


2 Answers

Adding a class with a width of 90% to your input element and adding the following input-icon class to your span would achieve what you want I think.

.input { width: 90%; } .input-icon {     display: inline-block;     height: 22px;     width: 22px;     line-height: 22px;     text-align: center;     color: #000;     font-size: 12px;     font-weight: bold;     margin-left: 4px; } 

EDIT Per dan's suggestion, it would not be wise to use .input as the class name, some more specific would be advised. I was simply using .input as a generic placeholder for your css

like image 27
Jim Avatar answered Sep 21 '22 23:09

Jim


Here are three different ways to do it:

screenshot

Here's a working Demo in Fiddle Of All Three

Validation:

You can use native bootstrap validation states (No Custom CSS!):

<div class="form-group has-feedback">     <label class="control-label" for="inputSuccess2">Name</label>     <input type="text" class="form-control" id="inputSuccess2"/>     <span class="glyphicon glyphicon-search form-control-feedback"></span> </div> 

For a full discussion, see my answer to Add a Bootstrap Glyphicon to Input Box

Input Group:

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> 

For a full discussion, see my answer to adding Twitter Bootstrap icon to Input box

Unstyled Input Group:

You can still use .input-group for positioning but just override the default styling to make the two elements appear separate.

Use a normal input group but add the class input-group-unstyled:

<div class="input-group input-group-unstyled">     <input type="text" class="form-control" />     <span class="input-group-addon">         <i class="fa fa-search"></i>     </span> </div> 

Then change the styling with the following css:

.input-group.input-group-unstyled input.form-control {     -webkit-border-radius: 4px;        -moz-border-radius: 4px;             border-radius: 4px; } .input-group-unstyled .input-group-addon {     border-radius: 4px;     border: 0px;     background-color: transparent; } 

Also, these solutions work for any input size

like image 141
KyleMit Avatar answered Sep 22 '22 23:09

KyleMit