如何使用CSS根变量修改Bootstrap 5.3折叠面板图标颜色
如何用CSS根变量设置Bootstrap 5.3折叠面板图标颜色
我正在修改Bootstrap 5.3的折叠面板组件,想让图标颜色和通过CSS根变量
var(--body-text)设置的标题文本颜色保持一致。用固定颜色(比如#aaaaaa)时能生效,但想改用自定义根变量,避免后续修改时要改多处CSS。
解决方案
不能直接在data URI的SVG中使用CSS变量(因为data URI里的变量无法继承外部的根变量),换用currentColor让图标继承按钮的文本颜色,再把按钮的文本颜色绑定到你的根变量即可:
- 定义根变量(如果还没定义):
:root { --body-text: #6c757d; /* 替换为你实际的颜色值 */ }
- 重写折叠面板按钮样式:
/* 重置折叠按钮的图标,使用currentColor继承文本颜色 */ body .accordion-button::after { background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='currentColor'%3e%3cpath fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/%3e%3c/svg%3e"); } /* 确保展开状态的图标颜色与收起状态一致 */ body .accordion-button:not(.collapsed)::after { background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='currentColor'%3e%3cpath fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/%3e%3c/svg%3e"); } /* 将折叠按钮的文本颜色绑定到根变量,图标自动继承该颜色 */ body .accordion-button { color: var(--body-text); } /* 确保展开状态下的文本颜色与根变量一致 */ body .accordion-button:not(.collapsed) { color: var(--body-text); }
- HTML代码(无需修改):
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous"> <div class="accordion" id="accordionExample"> <div class="accordion-item"> <h2 class="accordion-header"> <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> Accordion Item #1 </button> </h2> <div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#accordionExample"> <div class="accordion-body"> <strong>This is the first item's accordion body.</strong> It is shown 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 class="accordion-item"> <h2 class="accordion-header"> <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"> Accordion Item #2 </button> </h2> <div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#accordionExample"> <div class="accordion-body"> <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 class="accordion-item"> <h2 class="accordion-header"> <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree"> Accordion Item #3 </button> </h2> <div id="collapseThree" class="accordion-collapse collapse" data-bs-parent="#accordionExample"> <div class="accordion-body"> <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>
原理说明
currentColor是CSS的特殊值,会自动继承当前元素的color属性值,这样图标颜色就和按钮文本颜色绑定。- 把按钮的
color设置为var(--body-text),后续只需修改:root中的--body-text值,就能同步更新按钮文本和图标的颜色,无需修改多处CSS代码。
内容的提问来源于stack exchange,提问作者redstang423
相关产品推荐
相关产品推荐

