Search input with an icon Bootstrap 4 with rounded

2019-07-29 13:48发布

问题:

I want rounded border search box with search icon.

Below code works but if I class "rounded-pill" then icon separates from textbox.

How can I make textbox round while icon also remains within.

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">

<div class="container">
  <div class="row">
    <div class="col-md-4 mt-5">
      <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">
                    <button class="btn btn-outline-secondary border-left-0 border" type="button">
                        <i class="fa fa-search"></i>
                    </button>
                  </span>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-md-4 mt-5">
      <div class="input-group">
        <input class="form-control rounded-pill 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>
    </div>
  </div>
</div>

回答1:

Here's a Bootstrap only solution...

Use the negative margin utils to slide the button to the left:

 <div class="input-group">
     <input class="form-control py-2 rounded-pill mr-1 pr-5" type="search" value="search">
     <span class="input-group-append">
         <button class="btn rounded-pill border-0 ml-n5" type="button">
               <i class="fa fa-search"></i>
         </button>
     </span>
 </div>

This also works using input-group-text, or using the grid col-auto method explained in my other answer:

    <div class="row no-gutters mt-3 align-items-center">
        <div class="col-4">
            <input class="form-control border-secondary rounded-pill pr-5" type="search" value="search" id="example-search-input2">
        </div>
        <div class="col-auto">
            <button class="btn btn-outline-light text-dark border-0 rounded-pill ml-n5" type="button">
                <i class="fa fa-search"></i>
            </button>
        </div>
    </div>

https://www.codeply.com/go/cDQQcNY8kP



回答2:

I'm not 100% sure, but accordingly to my research Bootstrap 4.3.1 (last version today) do not allow to get what you want with only pre-defined classes of Bootstrap.

Here is a sample with some CSS classes that you can include into your code, and will allows you to get the desired result. Review the Snippet!

/* Rounded pill classes for horizontal sides */
.rounded-pill-left {
  border-top-left-radius: 50rem !important;
  border-bottom-left-radius: 50rem !important;
}
.rounded-pill-right {
  border-top-right-radius: 50rem !important;
  border-bottom-right-radius: 50rem !important;
}

/* Another classes to use */
.rounded-t-l-0 {
  border-top-left-radius: 0 !important;
}
.rounded-t-r-0 {
  border-top-right-radius: 0 !important;
}
.rounded-b-l-0 {
  border-bottom-left-radius: 0 !important;
}
.rounded-b-r-0 {
  border-bottom-right-radius: 0 !important;
}
.rounded-x-l-0 {
  border-top-left-radius: 0 !important;
  border-bottom-left-radius: 0 !important;
}
.rounded-x-r-0 {
  border-top-right-radius: 0 !important;
  border-bottom-right-radius: 0 !important;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">

<div class="container">
  <div class="row">
    <div class="col-md-4 mt-5">
      <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">
          <button class="btn btn-outline-secondary border-left-0 border" type="button">
              <i class="fa fa-search"></i>
          </button>
        </span>
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col-md-4 mt-5">
      <div class="input-group">
        <input class="form-control border rounded-pill-left border-right-0" type="search" value="search" id="example-search-input">
        <span class="input-group-append">
          <button class="btn btn-outline-secondary border rounded-pill-right border-left-0" type="button">
              <i class="fa fa-search"></i>
          </button>
        </span>
      </div>
    </div>
  </div>
</div>



标签: bootstrap-4