【问题标题】:Search input with an icon Bootstrap 4 with rounded textbox使用带有圆形文本框的图标 Bootstrap 4 搜索输入
【发布时间】:2019-08-14 15:38:47
【问题描述】:

我想要带有搜索图标的圆角搜索框。

以下代码有效,但如果我将“圆形药丸”分类,则图标与文本框分开。

如何使文本框变圆,而图标也保留在其中。

<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>

【问题讨论】:

    标签: bootstrap-4


    【解决方案1】:

    这是一个仅限 Bootstrap 的解决方案...

    使用负边距工具将按钮向左滑动:

     <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>
    

    这也适用于使用input-group-text,或使用my other answer 中解释的网格col-auto 方法:

        <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】:

      我不是 100% 确定,但根据我的研究,Bootstrap 4.3.1(今天的最新版本)不允许仅使用预定义的 Bootstrap 类来获得您想要的。

      这是一个包含一些 CSS 类的示例,您可以将这些类包含在您的代码中,并且可以让您获得所需的结果。查看代码段!

      /* 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>

      【讨论】:

      • 如果其他人不提供仅引导程序的解决方案,我会将其标记为答案。
      • 好的,谢谢。我希望有人为社区带来理想的解决方案!
      猜你喜欢
      • 2018-01-23
      • 2017-09-29
      • 2021-10-09
      • 1970-01-01
      • 1970-01-01
      • 2020-03-17
      • 2018-09-01
      • 1970-01-01
      相关资源
      最近更新 更多