简体   繁体   English

Bootstrap 4选择样式

[英]Bootstrap 4 select styling

In my Bootstrap v4 forms I increased the padding of my form-control elements but I noticed that this padding isn't being applied to the select element. 在我的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> 

Unfortunately I can't build a custom version of Bootstrap so I have to override the behaviour by applying pure css. 不幸的是,我无法构建Bootstrap的自定义版本,因此我必须通过应用纯CSS来覆盖行为。

I have created a JSFiddle that demonstrates the problem. 我创建了一个演示问题的JSFiddle

How can I correctly apply my padding to the select element in a Bootstrap v4 form? 如何在Bootstrap v4表单中将填充正确地应用到select元素?

The way you are trying to apply padding to Bootstrap 4 form-control elements is incorrect. 您尝试将填充应用于Bootstrap 4 form-control元素的方式不正确。 Because Bootstrap 4 has native classes specifically designed to satisfy all your spacing needs. 因为Bootstrap 4具有专门设计用于满足您所有间距需求的本机类。 And also because applying css hacks like that can (and often does) lead to problems that require even more css hacks to fix the problems caused by the original css hacks. 并且还因为这样的css hack可以(而且经常)导致导致需要更多css hack来解决由原始css hack引起的问题的问题。

So, use p-* and m-* classes to apply spacing to your elements and avoid unnecessary hacks. 因此,请使用p-*m-*类为元素应用间距,并避免不必要的破坏。

More info about Bootstrap 4 spacing utilities: 有关Bootstrap 4间隔实用程序的更多信息:

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

EDIT: 编辑:

To fix your issue with custom css, you'll need to add the following rules: 要解决自定义css的问题,您需要添加以下规则:

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

Here's the full, working snippet (click "run code snippet" and expand to full screen to compare side to side): 这是完整的有效代码段(单击“运行代码段”并展开到全屏以进行左右比较):

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

EDIT 2: 编辑2:

An even better solution would be to use a .custom-select because the normal select is inherently buggy. 更好的解决方案是使用.custom-select因为普通的选择本质上是越野车。 The custom-select was specifically designed for easy styling because the normal select is so buggy (and there isn't much that can be done about it). custom-select是专门为易于样式设计的,因为普通选择是如此之多(没有太多可做的事情)。

Here's a code snippet with custom-select for comparison: 这是一个带有自定义选择的代码段,用于比较:

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

Padding is being applied correctly, but you have to change the height 填充方式正确,但是您必须更改高度

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

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

Use more specificity on the CSS selector to override the Bootstrap height: 在CSS选择器上使用更多特性来覆盖Bootstrap高度:

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

The select will be the same height as the text inputs (assuming equal padding, borders, etc.. are applied to all the inputs). 选择的高度将与文本输入的高度相同(假设所有输入均使用相同的填充,边框等)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM