Bootstrap 4选择样式

时间:2018-01-30 08:45:32

标签: html css bootstrap-4 html-select

在我的Bootstrap v4表单中,我增加了form-control个元素的填充,但我注意到这个填充不适用于select元素。



.form-control {
  padding: .6rem .8rem !important;
  border: 2px solid #ced4da !important;
  transition: none !important;
}

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>

<div class="container-fluid">
  <div class="row">
    <div class="col-md-12">
      <form>
        <div class="form-row">
          <div class="form-group col-md-6">
            <label for="inputEmail4">Email</label>
            <input type="email" class="form-control" id="inputEmail4" placeholder="Email">
          </div>
          <div class="form-group col-md-6">
            <label for="inputPassword4">Password</label>
            <input type="password" class="form-control" id="inputPassword4" placeholder="Password">
          </div>
          <div class="form-group col-md-6">
            <label for="inputState">State</label>
            <select id="inputState" class="form-control">
              <option selected>Choose...</option>
              <option>...</option>
            </select>
          </div>
        </div>
        <button type="submit" class="btn btn-primary">Sign in</button>
      </form>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

不幸的是,我无法构建自定义版本的Bootstrap,所以我必须通过应用纯CSS来覆盖行为。

我创建了一个JSFiddle来演示问题。

如何正确地将填充应用于Bootstrap v4表单中的select元素?

3 个答案:

答案 0 :(得分:2)

尝试将填充应用于Bootstrap 4 form-control元素的方式不正确。因为Bootstrap 4具有专门设计的本机类,以满足您的所有间距需求。而且因为应用这样的css hacks可以(并且经常会)导致需要更多css hacks来修复原始css hacks引起的问题的问题。

因此,使用p-*m-*类可以为元素应用间距,避免不必要的黑客攻击。

有关Bootstrap 4间距工具的更多信息:

https://getbootstrap.com/docs/4.0/utilities/spacing/

编辑:

要解决自定义css的问题,您需要添加以下规则:

&#13;
&#13;
select.form-control{
    height: auto !important;
    padding: .6rem .8rem calc(.6rem + 1px) .8rem !important;
}
&#13;
&#13;
&#13;

这是完整的工作代码段(点击&#34;运行代码段&#34;然后展开到全屏幕进行比较):

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
<style>
.form-control {
    padding: .6rem .8rem !important;
    border: 2px solid #ced4da !important;
    transition: none !important;
}
select.form-control{
    height: auto !important;
    padding: .6rem .8rem calc(.6rem + 1px) .8rem !important;
}
</style>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <form>
                <div class="form-row">
                    <div class="form-group col-md-6">
                        <label for="inputEmail4">Email</label>
                        <input type="email" class="form-control" id="inputEmail4" placeholder="Email">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="inputPassword4">Password</label>
                        <input type="password" class="form-control" id="inputPassword4" placeholder="Password">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="inputState">State</label>
                        <select id="inputState" class="form-control">
                            <option selected>Choose...</option>
                            <option>...</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="inputEmail4">Test</label>
                        <input type="email" class="form-control" id="inputEmail4" placeholder="Choose... (normal input for comparison)">
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Sign in</button>
            </form>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

编辑2:

更好的解决方案是使用.custom-select,因为正常选择本质上是错误的。 custom-select是专为轻松设计而设计的,因为正常选择是如此错误(并且没有太多可以做到的事情)。

这是一个带有自定义选择的代码片段,用于比较:

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    
<style>
.form-control {
    padding: .6rem .8rem !important;
    border: 2px solid #ced4da !important;
    transition: none !important;
}
select.form-control{
    height: auto !important;
    padding: .6rem .8rem calc(.6rem + 1px) .8rem !important;
}
    
.custom-select {
    height: auto !important;
    padding: .6rem .8rem !important;
    border: 2px solid #ced4da !important;
    transition: none !important;
}
</style>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <form>
                <div class="form-row">
                    <div class="form-group col-md-6">
                        <label for="inputEmail4">Email</label>
                        <input type="email" class="form-control" id="inputEmail4" placeholder="Email">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="inputPassword4">Password</label>
                        <input type="password" class="form-control" id="inputPassword4" placeholder="Password">
                    </div>
                    <div class="form-group col-md-6">
                        <label for="inputState">State</label>
                        <select id="inputState" class="form-control">
                            <option selected>Choose...</option>
                            <option>...</option>
                        </select>
                    </div>
                    <div class="form-group col-md-6">
                        <label for="inputEmail4">Test</label>
                        <input type="email" class="form-control" id="inputEmail4" placeholder="Choose... (normal input for comparison)">
                    </div>
                    
                    <div class="form-group col-md-6">
                        <label for="inputState">custom-select:</label>
                        <div class="input-group">
                            <select class="custom-select" id="inputGroupSelect04">
                                <option selected>Choose... (custom-select)</option>
                                <option value="1">One</option>
                                <option value="2">Two</option>
                                <option value="3">Three</option>
                            </select>
                        </div>
                    </div>
                    
                    <div class="form-group col-md-6">
                        <label for="inputEmail4">Test</label>
                        <input type="email" class="form-control" id="inputEmail4" placeholder="Choose... (normal input for comparison)">
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Sign in</button>
            </form>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

正确应用了填充,但您必须更改高度

select.form-control{
  height:auto !important;
}

https://jsfiddle.net/oon0pmup/3/

答案 2 :(得分:1)

在CSS选择器上使用更多特异性来覆盖Bootstrap高度:

select.form-control:not([size]):not([multiple]) {
    height: initial;
}

select将与文本输入的高度相同(假设相等的填充,边框等应用于所有输入)。

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