Build vertically collapsing accordions with using Alpine Javascript.
<div class="w-full">
<div class="overflow-hidden border border-gray-500 rounded-lg" x-data="{selected:1}">
<ul class="shadow-box">
<li class="relative rounded-t-lg bg-[#e7ebfc]">
<button type="button" class="w-full px-6 py-4 text-gray-500 transition-all"
@click="selected == 1 ? selected = null : selected = 1"
x-bind:style="selected == 1 ? 'color: #2e46ba; background-color:#d8ddfa;box-shadow: 0 0.125rem 0.25rem 0rem #3a57e84a;' : 'color: grey;'">
<div class="flex items-center justify-between">
<span>Accordion Item #1</span>
<span>
<svg xmlns="http://www.w3.org/2000/svg"
x-bind:style="selected == 1 ? 'transform: rotate(180deg)' : ''"
class="text-gray-600 w-7 h-7" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd"
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
clip-rule="evenodd"></path>
</svg>
</span>
</div>
</button>
<div class="relative overflow-hidden transition-all max-h-0" style="" x-ref="container1"
x-bind:style="selected == 1 ? 'max-height: ' + $refs.container1.scrollHeight + 'px' : ''">
<div class="px-6 py-4 text-gray-600">
<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
<span class="text-pink-600">.accordion-body</span>, though the transition does limit
overflow.
</div>
</div>
</li>
<li class="relative border-t border-gray-500 bg-[#e7ebfc]">
<button type="button" class="w-full px-6 py-4 text-gray-500 transition-all"
@click="selected !== 2 ? selected = 2 : selected = null"
x-bind:style="selected == 2 ? 'color: #2e46ba; background-color:#d8ddfa; box-shadow: 0 0.125rem 0.25rem 0rem #3a57e84a;' : 'color: grey;'">
<div class="flex items-center justify-between">
<span>Accordion Item #2</span>
<span>
<svg xmlns="http://www.w3.org/2000/svg"
x-bind:style="selected == 2 ? 'transform: rotate(180deg)' : ''"
class="text-gray-600 w-7 h-7" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd"
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
clip-rule="evenodd"></path>
</svg>
</span>
</div>
</button>
<div class="relative overflow-hidden transition-all max-h-0" style="" x-ref="container2"
x-bind:style="selected == 2 ? 'max-height: ' + $refs.container2.scrollHeight + 'px' : ''">
<div class="px-6 py-4 text-gray-600">
<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
<span class="text-pink-600">.accordion-body</span>, though the transition does limit
overflow.
</div>
</div>
</li>
<li class="relative transition-all border-t border-gray-500 rounded-b-lg bg-[#e7ebfc]">
<button type="button" class="w-full px-6 py-4 text-gray-500"
@click="selected !== 3 ? selected = 3 : selected = null"
x-bind:style="selected == 3 ? 'color: #2e46ba; background-color:#d8ddfa;box-shadow: 0 0.125rem 0.25rem 0rem #3a57e84a;' : 'color: grey;'">
<div class="flex items-center justify-between">
<span>Accordion Item #3</span>
<span>
<svg xmlns="http://www.w3.org/2000/svg"
x-bind:style="selected == 3 ? 'transform: rotate(180deg)' : ''"
class="text-gray-600 w-7 h-7" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd"
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
clip-rule="evenodd"></path>
</svg>
</span>
</div>
</button>
<div class="relative overflow-hidden transition-all max-h-0" style="" x-ref="container3"
x-bind:style="selected == 3 ? 'max-height: ' + $refs.container3.scrollHeight + 'px' : ''">
<div class="py-4 px-6 text-gray-600">
<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
<span class="text-pink-600">.accordion-body</span>, though the transition does limit
overflow.
</div>
</div>
</li>
</ul>
</div>
</div>
Add .custom-accordion
to remove the default
background-color, shadow
, some borders, and some rounded corners to
render accordions edge-to-edge with their parent container.
<div class="block" data-tab-content="true" id="accordiansection">
<div class="w-full">
<div class="overflow-hidden rounded-lg" x-data="{selected:1}">
<ul class="shadow-box">
<li class="relative rounded-t-lg">
<button type="button" class="w-full px-6 py-4 text-secondary-500 transition-all mt-4"
@click="selected == 1 ? selected = null : selected = 1"
x-bind:style="selected == 1 ? 'color: #2e46ba;' : 'color: grey;'">
<div class="flex items-center justify-between">
<span>Accordion Item #1</span>
<span>
<svg xmlns="http://www.w3.org/2000/svg"
x-bind:style="selected == 1 ? 'color: blue; transform: rotate(-180deg);transition-duration: 500ms' : 'transform: -rotate(180deg);transition-duration: 500ms'"
class="text-secondary-600 w-7 h-7 active:stroke-primary-500" viewBox="0 0 20 20"
fill="currentColor">
<path fill-rule="evenodd"
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
clip-rule="evenodd"></path>
</svg>
</span>
</div>
</button>
<div class="relative overflow-hidden transition-all max-h-0" x-ref="container1"
x-bind:style="selected == 1 ? 'max-height: ' + $refs.container1.scrollHeight + 'px' : ''">
<div class="px-6 py-4 text-slate-400 dark:bg-dark-card">
<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
<span class="text-pink-600">.accordion-body</span>, though the transition does limit
overflow.
</div>
</div>
</li>
<li class="relative">
<button type="button" class="w-full px-6 py-4 text-secondary-500 transition-all"
@click="selected !== 2 ? selected = 2 : selected = null"
x-bind:style="selected == 2 ? 'color: #2e46ba;' : 'color: grey;'">
<div class="flex items-center justify-between">
<span>Accordion Item #2</span>
<span>
<svg xmlns="http://www.w3.org/2000/svg"
x-bind:style="selected == 2 ? 'color: blue; transform: rotate(-180deg);transition-duration: 500ms' : 'transform: -rotate(180deg);transition-duration: 500ms'"
class="text-secondary-600 w-7 h-7 active:stroke-primary-500" viewBox="0 0 20 20"
fill="currentColor">
<path fill-rule="evenodd"
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
clip-rule="evenodd"></path>
</svg>
</span>
</div>
</button>
<div class="relative overflow-hidden transition-all max-h-0" x-ref="container2"
x-bind:style="selected == 2 ? 'max-height: ' + $refs.container2.scrollHeight + 'px' : ''">
<div class="px-6 py-4 text-slate-400 dark:bg-dark-card">
<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
<span class="text-pink-600">.accordion-body</span>, though the transition does limit
overflow.
</div>
</div>
</li>
<li class="relative transition-all rounded-b-lg">
<button type="button" class="w-full px-6 py-4 text-secondary-500"
@click="selected !== 3 ? selected = 3 : selected = null"
x-bind:style="selected == 3 ? 'color: #2e46ba;' : 'color: grey;'">
<div class="flex items-center justify-between">
<span>Accordion Item #3</span>
<span>
<svg xmlns="http://www.w3.org/2000/svg"
x-bind:style="selected == 3 ? 'color: blue; transform: rotate(-180deg);transition-duration: 500ms' : 'transform: -rotate(180deg);transition-duration: 500ms'"
class="text-secondary-600 w-7 h-7 active:stroke-primary-500" viewBox="0 0 20 20"
fill="currentColor">
<path fill-rule="evenodd"
d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
clip-rule="evenodd"></path>
</svg>
</span>
</div>
</button>
<div class="relative overflow-hidden transition-all max-h-0" style="" x-ref="container3"
x-bind:style="selected == 3 ? 'max-height: ' + $refs.container3.scrollHeight + 'px' : ''">
<div class="py-4 px-6 text-slate-400 dark:bg-dark-card">
<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
<span class="text-pink-600">.accordion-body</span>, though the transition does limit
overflow.
</div>
</div>
</li>
</ul>
</div>
</div>
</div>