CSS 为 Bootstrap select 的 selectpicker 版本设计样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40960505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Styling the selectpicker version of Bootstrap select
提问by Bill Noble
I am trying, and failing, to style the boostrap-select widget. I have tried the following code but it does not change the background and text color in the selectpicker select box:
我正在尝试设置 boostrap-select 小部件的样式,但失败了。我尝试了以下代码,但它不会更改 selectpicker 选择框中的背景和文本颜色:
<select id="what" class="wia-filter-value selectpicker show-tick">
<option selected>All</option>
<option>Events</option>
<option>Man Made</option>
<option>Nature</option>
</select>
My CSS looks like this:
我的 CSS 看起来像这样:
select {
width: 200px;
height: 30px;
background-color: #141414 !important;
border-style: solid;
border-left-width: 3px;
border-left-color: #00DDDD;
border-top: none;
border-bottom: none;
border-right: none;
color: white;
font-size: 18px;
font-weight: 200;
padding-left: 6px;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.selectpicker{
width: 200px;
height: 30px;
background-color: #141414 !important;
border-style: solid;
border-left-width: 3px;
border-left-color: #00DDDD;
border-top: none;
border-bottom: none;
border-right: none;
color: white;
font-size: 18px;
font-weight: 200;
padding-left: 6px;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
select::-ms-expand { /* for IE 11 */
display: none;
}
.wia-filter-value {
width: 200px;
height: 30px;
background-color: #141414 !important;
border-style: solid;
border-left-width: 3px;
border-left-color: #00DDDD;
border-top: none;
border-bottom: none;
border-right: none;
color: white;
font-size: 18px;
font-weight: 200;
padding-left: 6px;
}
This CSS successfully applies the left border color but does not apply the background color or the text color.
此 CSS 成功应用了左边框颜色,但未应用背景颜色或文本颜色。
NOTE:The solution show in stackoverflow at enter link description hereis for the standard Boostrap select. I am using the selectpicker widget from https://silviomoreto.github.io/bootstrap-select. That solution does not appear to work for selectpicker, or at least I cannot get it to work.
注意:stackoverflow 中在此处输入链接描述处显示的解决方案适用于标准 Boostrap 选择。我正在使用https://silviomoreto.github.io/bootstrap-select 中的 selectpicker 小部件。该解决方案似乎不适用于 selectpicker,或者至少我无法让它工作。
回答by vanburen
You need to target the Bootstrap-selectclasses the plugin is applying. If you inspect the element you can see what's going on. Here's a screenshot for reference as to what is being applied.
您需要定位插件正在应用的Bootstrap-select类。如果您检查元素,您可以看到发生了什么。这是有关正在应用的内容的参考屏幕截图。
Working Example:
工作示例:
body {
padding-top: 50px;
}
.bootstrap-select {
max-width: 200px;
}
.bootstrap-select .btn {
background-color: #141414;
border-style: solid;
border-left-width: 3px;
border-left-color: #00DDDD;
border-top: none;
border-bottom: none;
border-right: none;
color: white;
font-weight: 200;
padding: 12px 12px;
font-size: 16px;
margin-bottom: 10px;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.bootstrap-select .dropdown-menu {
margin: 15px 0 0;
}
select::-ms-expand {
/* for IE 11 */
display: none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css" rel="stylesheet" />
<div class="container text-center">
<select id="what" class="form-control selectpicker show-tick">
<option selected>All</option>
<option>Events</option>
<option>Man Made</option>
<option>Nature</option>
</select>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
回答by Jimmy Mendieta
<div class="form-group">
<label class="control-label col-md-4">Theme White</label>
<div class="col-md-8">
<select class="form-control selectpicker" data-size="10" data-live-search="true" data-style="btn-white">
<option value="" selected>Select a Country</option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AS">American Samoa</option>
<option value="AD">Andorra</option>
<option value="AO">Angola</option>
<option value="AI">Anguilla</option>
<option value="AQ">Antarctica</option>
<option value="AG">Antigua and Barbuda</option>
</select>
</div>
</div>
Add data-style="btn-white"
for color white
添加data-style="btn-white"
颜色为白色