如何使手风琴中的手风琴项目周围有一致的边框
How to make a accordion-item in an accordion have a consistent border around it
我正在尝试为一个项目制作手风琴。我正在使用 bootstrap 并且我想让我的手风琴物品具有光滑的圆角和边框。
html:
<div class="container w-50">
<div class="accordion" id="accordionFlushExample">
<div class="accordion-item mb-2">
<h2 class="accordion-header mb-0" id="flush-headingOne">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseOne" aria-expanded="false" aria-controls="flush-collapseOne">
Question #1
</button>
</h2>
<div id="flush-collapseOne" class="accordion-collapse collapse" aria-labelledby="flush-headingOne" data-bs-parent="#accordionFlushExample">
<div class="accordion-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Animi id, earum quis mollitia illum beatae laboriosam ipsa architecto eaque,
deleniti neque veniam odit quas laudantium dolor, nobis molestiae quibusdam assumenda.</div>
</div>
</div>
<div class="accordion-item mb-2">
<h2 class="accordion-header mb-0" id="flush-headingTwo">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseTwo" aria-expanded="false" aria-controls="flush-collapseTwo">
Question #2
</button>
</h2>
<div id="flush-collapseTwo" class="accordion-collapse collapse" aria-labelledby="flush-headingTwo" data-bs-parent="#accordionFlushExample">
<div class="accordion-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Animi id, earum quis mollitia illum beatae laboriosam ipsa architecto eaque,
deleniti neque veniam odit quas laudantium dolor, nobis molestiae quibusdam assumenda.</div>
</div>
</div>
<div class="accordion-item mb-2">
<h2 class="accordion-header mb-0" id="flush-headingThree">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseThree" aria-expanded="false" aria-controls="flush-collapseThree">
Question #3
</button>
</h2>
<div id="flush-collapseThree" class="accordion-collapse collapse" aria-labelledby="flush-headingThree" data-bs-parent="#accordionFlushExample">
<div class="accordion-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Animi id, earum quis mollitia illum beatae laboriosam ipsa architecto eaque,
deleniti neque veniam odit quas laudantium dolor, nobis molestiae quibusdam assumenda.</div>
</div>
</div>
</div>
css:
.accordion-item{
font-size: 0.5em;
border: 0.2em solid var(--white);
border-radius: 10px;
}
.accordion-item h2 button{
text-align: left;
text-decoration: none;
color: var(--background-col);
border-radius: 0;
box-shadow: none !important;
}
.accordion-item h2 button:hover{
text-decoration: none;
box-shadow: none !important;
}
.accordion-item h2 button.collapsed{
color: var(--background-col);
box-shadow: none !important;
}
.accordion-item .accordion-body{
padding-top: 0.5em;
}
.accordion-button:not(.collapsed){
color: var(--background-col);
background: var(--white);
border: none;
}
.accordion-button:focus{
border-color: var(--white);
}
通过这个“设置”,我设法让我的手风琴显示如下:
但这是不正确的。当我将边框设为红色(只是为了看到它)时,这就是我得到的:
我曾尝试为每个元素制作边框,只是为了看看是否可以用任何其他方式修复它,但我做不到,因为我希望边框在手风琴打开时保持一致。
我可以用 bootstrap 做吗,还是我应该自己从头开始做?
编辑:我有一个 codepen,我在其中演示了问题:https://codepen.io/BrainlessPOMO/pen/WNdzLXV
这与优先级有关。如果您使用 id selector
或使用 bootstrap 中的 类 来定位手风琴的第一个和最后一个元素,因为这将覆盖 bootstrap 中的 'default',如下图
body {
padding: 2rem;
}
#accordionExample .accordion-item {
font-size: 0.5em;
border-top: 2em solid blue;
border-bottom: 2em solid blue;
border-left: 2em solid blue;
border-right: 2em solid blue;
border-radius: 14px;
}
.accordion .accordion-item + .accordion-item {
margin-top: 2rem;
border-top: 2em solid blue;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<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" aria-labelledby="headingOne" 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" id="headingTwo">
<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" aria-labelledby="headingTwo" 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" id="headingThree">
<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" aria-labelledby="headingThree" 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>
我正在尝试为一个项目制作手风琴。我正在使用 bootstrap 并且我想让我的手风琴物品具有光滑的圆角和边框。
html:
<div class="container w-50">
<div class="accordion" id="accordionFlushExample">
<div class="accordion-item mb-2">
<h2 class="accordion-header mb-0" id="flush-headingOne">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseOne" aria-expanded="false" aria-controls="flush-collapseOne">
Question #1
</button>
</h2>
<div id="flush-collapseOne" class="accordion-collapse collapse" aria-labelledby="flush-headingOne" data-bs-parent="#accordionFlushExample">
<div class="accordion-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Animi id, earum quis mollitia illum beatae laboriosam ipsa architecto eaque,
deleniti neque veniam odit quas laudantium dolor, nobis molestiae quibusdam assumenda.</div>
</div>
</div>
<div class="accordion-item mb-2">
<h2 class="accordion-header mb-0" id="flush-headingTwo">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseTwo" aria-expanded="false" aria-controls="flush-collapseTwo">
Question #2
</button>
</h2>
<div id="flush-collapseTwo" class="accordion-collapse collapse" aria-labelledby="flush-headingTwo" data-bs-parent="#accordionFlushExample">
<div class="accordion-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Animi id, earum quis mollitia illum beatae laboriosam ipsa architecto eaque,
deleniti neque veniam odit quas laudantium dolor, nobis molestiae quibusdam assumenda.</div>
</div>
</div>
<div class="accordion-item mb-2">
<h2 class="accordion-header mb-0" id="flush-headingThree">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseThree" aria-expanded="false" aria-controls="flush-collapseThree">
Question #3
</button>
</h2>
<div id="flush-collapseThree" class="accordion-collapse collapse" aria-labelledby="flush-headingThree" data-bs-parent="#accordionFlushExample">
<div class="accordion-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit.
Animi id, earum quis mollitia illum beatae laboriosam ipsa architecto eaque,
deleniti neque veniam odit quas laudantium dolor, nobis molestiae quibusdam assumenda.</div>
</div>
</div>
</div>
css:
.accordion-item{
font-size: 0.5em;
border: 0.2em solid var(--white);
border-radius: 10px;
}
.accordion-item h2 button{
text-align: left;
text-decoration: none;
color: var(--background-col);
border-radius: 0;
box-shadow: none !important;
}
.accordion-item h2 button:hover{
text-decoration: none;
box-shadow: none !important;
}
.accordion-item h2 button.collapsed{
color: var(--background-col);
box-shadow: none !important;
}
.accordion-item .accordion-body{
padding-top: 0.5em;
}
.accordion-button:not(.collapsed){
color: var(--background-col);
background: var(--white);
border: none;
}
.accordion-button:focus{
border-color: var(--white);
}
通过这个“设置”,我设法让我的手风琴显示如下:
但这是不正确的。当我将边框设为红色(只是为了看到它)时,这就是我得到的:
我曾尝试为每个元素制作边框,只是为了看看是否可以用任何其他方式修复它,但我做不到,因为我希望边框在手风琴打开时保持一致。 我可以用 bootstrap 做吗,还是我应该自己从头开始做?
编辑:我有一个 codepen,我在其中演示了问题:https://codepen.io/BrainlessPOMO/pen/WNdzLXV
这与优先级有关。如果您使用 id selector
或使用 bootstrap 中的 类 来定位手风琴的第一个和最后一个元素,因为这将覆盖 bootstrap 中的 'default',如下图
body {
padding: 2rem;
}
#accordionExample .accordion-item {
font-size: 0.5em;
border-top: 2em solid blue;
border-bottom: 2em solid blue;
border-left: 2em solid blue;
border-right: 2em solid blue;
border-radius: 14px;
}
.accordion .accordion-item + .accordion-item {
margin-top: 2rem;
border-top: 2em solid blue;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<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" aria-labelledby="headingOne" 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" id="headingTwo">
<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" aria-labelledby="headingTwo" 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" id="headingThree">
<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" aria-labelledby="headingThree" 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>