简体   繁体   English

Bootstrap btn-group 改变颜色

[英]Bootstrap btn-group change color

How can I change the background color of the active/selected button in the button group?如何更改按钮组中活动/选定按钮的背景颜色?

<div class="btn-group" role="group" aria-label="Basic radio toggle button group">
  <input type="radio" class="btn-check" name="btnradio" id="btnradio1" checked>
  <label id="monthly" class="btn btn-outline-primary" for="btnradio1">Button 1</label>
        
  <input type="radio" class="btn-check" name="btnradio" id="btnradio2">
  <label class="btn btn-outline-primary" for="btnradio2">Button 2</label>
</div>

By using this css, the unselected btn color gets changed.通过使用这个 css,未选中的 btn 颜色会发生变化。 But how can I change the background color of the selected btn?但是如何更改所选 btn 的背景颜色?

.btn-group .btn {
 color: #ff60bd;
 border-color: #ff60bd;
}

Many thanks!非常感谢!

Add this code in your css file and try.将此代码添加到您的 css 文件中并尝试。 First is to hide the radio controls, but second is I think what you need if you want to change the background color when button is :checked首先是隐藏单选控件,但第二个是我认为如果你想在按钮被选中时更改背景颜色,你需要什么

input[type="radio"]{
    display: none;
}

input[type="radio"]:checked + .btn {
    color: #fff;
    background: #ff60bd!important;
}

input[type="radio"]:hover + .btn {
  color: #fff;
  background-color: red;
}

The selected or active button has active class, you need to check ".active" class of ".btn" as following sample:选中或激活的按钮有激活的 class,您需要检查“.btn”的“.active” class,如下示例:

.btn-group .btn.active {
 color: #ff60bd;
 border-color: #ff60bd;
}

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

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