Put search icon near textbox using bootstrap

前端 未结 5 1664
清酒与你
清酒与你 2020-12-02 08:51

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:



        
相关标签:
5条回答
  • 2020-12-02 09:15

    Here are three different ways to do it:

    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

    0 讨论(0)
  • 2020-12-02 09:17
    <input type="text" name="whatever" id="funkystyling" />
    

    Here's the CSS for the image on the left:

    #funkystyling {
        background: white url(/path/to/icon.png) left no-repeat;
        padding-left: 17px;
    }
    

    And here's the CSS for the image on the right:

    #funkystyling {
        background: white url(/path/to/icon.png) right no-repeat;
        padding-right: 17px;
    }
    
    0 讨论(0)
  • 2020-12-02 09:24

    You can do it in pure CSS using the :after pseudo-element and getting creative with the margins.

    Here's an example, using Font Awesome for the search icon:

    .search-box-container input {
      padding: 5px 20px 5px 5px;
    }
    
    .search-box-container:after {
        content: "\f002";
        font-family: FontAwesome;
        margin-left: -25px;
        margin-right: 25px;
    }
    <!-- font awesome -->
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
    
    
    <div class="search-box-container">
      <input type="text" placeholder="Search..."  />
    </div>

    0 讨论(0)
  • 2020-12-02 09:25

    I liked @KyleMit's answer on how to make an unstyled input group, but in my case, I only wanted the right side unstyled - I still wanted to use an input-group-addon on the left side and have it look like normal bootstrap. So, I did this:

    css

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

    html

    <div class="input-group input-group-unstyled-right">
        <span class="input-group-addon">
            <i class="fa fa-envelope-o"></i>
        </span>
        <input type="text" class="form-control">
        <span class="input-group-addon input-group-addon-unstyled">
            <i class="fa fa-check"></i>
        </span>
    </div>
    
    0 讨论(0)
  • 2020-12-02 09:31

    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

    0 讨论(0)
提交回复
热议问题