Build an accordion with Alpine + Tailwind

Опубликовано: 29 Март 2026
на канале: codingoblin
575
13

How to build an accordion with Tailwind CSS and Alpine.js. Building our own accordions can be difficult. But thankfully Tailwind CSS and Alpine js make it simple to do.

The first thing we need to do is make use of Alpine's x-data directive to create an array containing our accordion items. Each item will have a title, some content and an open value (which is a boolean). We then loop over this array using another Alpine js directive, the x-for directive. This x-for directive is used on a "template" tag. In the Alpine loop we then output the values from the array. We also add a button that toggles the value of each "open" value in each item. This button will contain an arrow, or chevron, icon that changes depending on the accordion's open state. We use another Alpine.js directive, x-show. This is used to show the correct chevron and item content, dependant on the state of "open."

We can then style our Alpine JS accordion using Tailwind CSS to make it look nice!

First we create our html and add some Tailwind classes to make it look half decent.

If you want to build your own accordion, you will be able to do this easily by using Alpine and Tailwind!

My Hosting Provider - use code GOBLIN for a discount. Free trial with no card required:
https://platform.cloudways.com/signup...