Bootstrap - 选择和按钮彼此相邻

时间:2014-07-04 11:59:22

标签: css twitter-bootstrap

我有一个<select>和一个紧挨着的按钮,想要用引导程序对它们进行样式设置。 没有上课的Select看起来像这样:

enter image description here

带有select

class="form-control"如下所示: enter image description here

注意,该按钮现在下选择。我不确定,哪个CSS属性对此负责。我应该更改什么,在选择旁边有按钮?

5 个答案:

答案 0 :(得分:30)

您可以将<select><button>打包在input-group中:

<div class="input-group">
  <select class="form-control">
    <option>Select option..</option>
    <option>Option 1</option>
    <option>Option 2</option>
  </select>            
  <span class="input-group-btn">
    <button class="btn btn-default" type="button" tabindex="-1"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
  </span>
</div>

通过这种方式,您可以使用普通表单,只需将一个选择和按钮组合在一起。

查看JSFiddle example

答案 1 :(得分:12)

遵循Bootstrap语法:

您应该在表单中添加form-inline课程,请参阅official doc

官方示例

<form class="form-inline" role="form">
  <div class="form-group">
    <label class="sr-only" for="exampleInputEmail2">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail2" placeholder="Enter email">
  </div>
  <div class="form-group">
    <div class="input-group">
      <div class="input-group-addon">@</div>
      <input class="form-control" type="email" placeholder="Enter email">
    </div>
  </div>
  <div class="form-group">
    <label class="sr-only" for="exampleInputPassword2">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword2" placeholder="Password">
  </div>
  <div class="checkbox">
    <label>
      <input type="checkbox"> Remember me
    </label>
  </div>
  <button type="submit" class="btn btn-default">Sign in</button>
</form>

答案 2 :(得分:2)

另一种方法是将控件包装在flex div中:

<div style="display: flex;">
  <select class="form-control">...</select>
  <!-- optional spacer: <div style="width: 8px;" /> -->
  <button type="button" class="btn btn-default">Click Me!</button>
</div>

答案 3 :(得分:1)

引导程序4

使用助手类! d-flex就可以了。

body {

  padding-top: 20px;
}
<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">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<div class="container">
  <div class="row">
    <div class="col-12">

      <!-- Wrap your 2 elements in a d-flex -->

      <div class="d-flex">
        <select class="form-control mr-2">
          <option value="1">One</option>
          <option value="2">Two</option>
          <option value="3">Three</option>
        </select>
        <input type="button" class="btn btn-secondary" value="Select" />
      </div>

    </div>
  </div>
</div>

答案 4 :(得分:0)

如Luke所述,但是将input-group-append用于按钮范围。这样就消除了按钮上的圆度,并实现了更好的匹配。