Search input with an icon Bootstrap 4

后端 未结 6 1063
面向向阳花
面向向阳花 2020-12-01 00:43

No clue how I can do this, since BS 4 doesn\'t support glyphicons. Do I set it up as a background or do I apply different positioning to a font-awesome icon?

This i

相关标签:
6条回答
  • 2020-12-01 01:19

    I made another variant with dropdown menu (perhaps for advanced search etc).. Here is how it looks like:

    <div class="input-group my-4 col-6 mx-auto">
        <input class="form-control py-2 border-right-0 border" type="search" placeholder="Type something..." id="example-search-input">
        <span class="input-group-append">
            <button type="button" class="btn btn-outline-primary dropdown-toggle dropdown-toggle-split border border-left-0 border-right-0 rounded-0" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <span class="sr-only">Toggle Dropdown</span>
            </button>
            <button class="btn btn-outline-primary rounded-right" type="button">
                <i class="fas fa-search"></i>
            </button>
            <div class="dropdown-menu dropdown-menu-right">
                <a class="dropdown-item" href="#">Action</a>
                <a class="dropdown-item" href="#">Another action</a>
                <a class="dropdown-item" href="#">Something else here</a>
                <div role="separator" class="dropdown-divider"></div>
                <a class="dropdown-item" href="#">Separated link</a>
            </div>
        </span>
    </div>
    

    Note: It appears green in the screenshot because my site main theme is green.

    0 讨论(0)
  • 2020-12-01 01:23

    Update 2019

    Why not use an input-group?

    <div class="input-group col-md-4">
          <input class="form-control py-2" type="search" value="search" id="example-search-input">
          <span class="input-group-append">
            <button class="btn btn-outline-secondary" type="button">
                <i class="fa fa-search"></i>
            </button>
          </span>
    </div>
    

    And, you can make it appear inside the input using the border utils...

            <div class="input-group col-md-4">
                <input class="form-control py-2 border-right-0 border" type="search" value="search" id="example-search-input">
                <span class="input-group-append">
                  <button class="btn btn-outline-secondary border-left-0 border" type="button">
                        <i class="fa fa-search"></i>
                  </button>
                </span>
            </div>
    

    Or, using a input-group-text w/o the gray background so the icon appears inside the input...

            <div class="input-group">
                <input class="form-control py-2 border-right-0 border" type="search" value="search" id="example-search-input">
                <span class="input-group-append">
                    <div class="input-group-text bg-transparent"><i class="fa fa-search"></i></div>
                </span>
            </div>
    

    Alternately, you can use the grid (row>col-) with no gutter spacing:

    <div class="row no-gutters">
         <div class="col">
              <input class="form-control border-secondary border-right-0 rounded-0" type="search" value="search" id="example-search-input4">
         </div>
         <div class="col-auto">
              <button class="btn btn-outline-secondary border-left-0 rounded-0 rounded-right" type="button">
                 <i class="fa fa-search"></i>
              </button>
         </div>
    </div>
    

    Or, prepend the icon like this...

    <div class="input-group">
      <span class="input-group-prepend">
        <div class="input-group-text bg-transparent border-right-0">
          <i class="fa fa-search"></i>
        </div>
      </span>
      <input class="form-control py-2 border-left-0 border" type="search" value="..." id="example-search-input" />
      <span class="input-group-append">
        <button class="btn btn-outline-secondary border-left-0 border" type="button">
         Search
        </button>
      </span>
    </div>
    

    Demo of all Bootstrap 4 icon input options



    Example with validation icons

    0 讨论(0)
  • 2020-12-01 01:23

    Here is an input box with a search icon on the right.

      <div class="input-group">
          <input class="form-control py-2 border-right-0 border" type="search" placeholder="Search">
          <div class="input-group-append">
              <div class="input-group-text" id="btnGroupAddon2"><i class="fa fa-search"></i></div>
          </div>
      </div>
    

    Here is an input box with a search icon on the left.

      <div class="input-group">
          <div class="input-group-prepend">
              <div class="input-group-text" id="btnGroupAddon2"><i class="fa fa-search"></i></div>
          </div>
          <input class="form-control py-2 border-right-0 border" type="search" placeholder="Search">
      </div>
    
    0 讨论(0)
  • 2020-12-01 01:26

    in ASPX bootstrap v4.0.0, no beta (dl 21-01-2018)

    <div class="input-group">
    <asp:TextBox ID="txt_Product" runat="server" CssClass="form-control" placeholder="Product"></asp:TextBox>
    <div class="input-group-append">
        <asp:LinkButton ID="LinkButton3" runat="server" CssClass="btn btn-outline-primary">
            <i class="ICON-copyright"></i>
        </asp:LinkButton>
    </div>
    

    0 讨论(0)
  • 2020-12-01 01:34

    Here's a fairly simple way to achieve it by enclosing both the magnifying glass icon and the input field inside a div with relative positioning.

    Absolute positioning is applied to the icon, which takes it out of the normal document layout flow. The icon is then positioned inside the input. Left padding is applied to the input so that the user's input appears to the right of the icon.

    Note that this example places the magnifying glass icon on the left instead of the right. This is recommended when using <input type="search"> as Chrome adds an X button in the right side of the searchbox. If we placed the icon there it would overlay the X button and look fugly.

    Here is the needed Bootstrap markup.

    <div class="position-relative">
        <i class="fa fa-search position-absolute"></i>
        <input class="form-control" type="search">
    </div>
    

    ...and a couple CSS classes for the things which I couldn't do with Bootstrap classes:

    i {
        font-size: 1rem;
        color: #333;
        top: .75rem;
        left: .75rem
    }
    
    input {
        padding-left: 2.5rem;
    }
    

    You may have to fiddle with the values for top, left, and padding-left.

    0 讨论(0)
  • 2020-12-01 01:37

    you can also do in this way using input-group

    <div class="input-group">
      <input class="form-control"
             placeholder="I can help you to find anything you want!">
      <div class="input-group-addon" ><i class="fa fa-search"></i></div>
    </div>
    

    codeply

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