如何更改默认的Bootstrap手风琴按钮?
想知道是否有人知道如何更改 Bootstrap 4、5 手风琴的默认折叠按钮?
我想用 fontawesome 自定义它们。谢谢!
示例手风琴:
<div>
<div>
<h2>
<button type="button" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
</h2>
<div aria-labelledby="headingOne">
<div>
<strong>This is the first item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div>
<h2>
<button type="button" aria-expanded="false" aria-controls="collapseTwo">
Accordion Item #2
</button>
</h2>
<div aria-labelledby="headingTwo">
<div>
<strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div>
<h2>
<button type="button" aria-expanded="false" aria-controls="collapseThree">
Accordion Item #3
</button>
</h2>
<div aria-labelledby="headingThree">
<div>
<strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
</div>
回答
对于 Bootstrap 5,使用 CSS:
.accordion-button:not(.collapsed)::after {
background-image: url("data:image/svg+xml,...");
}
.accordion-button::after {
background-image: url("data:image/svg+xml,...");
}
(注意只能使用一张图片,因为默认的 CSS 用 来旋转它rotate(180deg))
有关使用 FA 的&的示例,请参阅此 JSFiddle。caret-upcaret-down
对于 Bootstrap 4——Accordion 组件不使用图像。
但是,如果您包含可以使用的 FA 字体文件(用于“+”和“-”):
.accordion .card-header:after {
font-family: 'FontAwesome';
content: 'f068';
float: right;
}
.accordion .card-header.collapsed:after {
content: 'f068';
}