Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="bg-gray-100 pt-10 px-3 pb-20">
  <h1 class="text-gray-600 text-center">
    <div class="font-bold">
      Smooth Accordion
    </div>
    using Alpine.js (w/ x-for) + Tailwind CSS
  </h1>

  <div class="my-10 max-w-2xl mx-auto space-y-4 lg:space-y-6" x-data="{ faq, faq_selected: null }">

    <template x-for="(item, index) in faq" :key="`item-{$index}`">

      <div class="item bg-white shadow-md rounded-md p-3">
        <div class="flex items-center cursor-pointer" @click="faq_selected !== index ? faq_selected = index : faq_selected = null">
          <div class="bg-indigo-100 text-indigo-400 w-8 h-8 md:w-10 md:h-10 rounded-md flex items-center justify-center font-bold text-lg font-display">
            <span x-text="index + 1"></span>
          </div>
          <div class="ml-3 md:ml-4 lg:ml-6 md:text-lg text-indigo-600">
            <span x-text="item.question"></span>
          </div>
        </div>
        <div class="relative overflow-hidden transition-all max-h-0 duration-700" x-bind:style="faq_selected === index ? `max-height:  ${ $el.scrollHeight }px` : ``">
          <div class="text-gray-700 ml-8 md:ml-10 pl-3 md:pl-4 lg:pl-6 py-2 space-y-3">

            <template x-for="(ans, index) in item.answer" :key="`item-ans-{$index}`">
              <p x-text="ans"></p>
            </template>

          </div>
        </div>
      </div>

    </template>

  </div>
</div>
              
            
!

CSS

              
                .max-h-0 {
  max-height: 0
}

/* 
Inspired by
https://codepen.io/QJan84/pen/zYvRMMw
*/
              
            
!

JS

              
                const faq = [
  {
    question: "What is Alpine.js?",
    answer: [
      "Bacon ipsum dolor amet boudin hamburger jerky spare ribs, bacon leberkas beef ribs sausage turkey pancetta tenderloin chicken.",
      "Meatball landjaeger turducken. Bacon bresaola tenderloin cow rump pork chop."
    ]
  },
  {
    question: "Is it difficult to learn?",
    answer: [
      "Boudin sausage hamburger tenderloin beef chislic prosciutto pancetta. Beef tongue pork meatloaf.",
      "Chicken pork chop turducken ground round. Shank bresaola burgdoggen short loin ham hock ham. Boudin tri-tip swine drumstick strip steak tail."
    ]
  },
  {
    question: "How can I use this code?",
    answer: [
      "Salami filet mignon strip steak venison rump chicken bresaola. Shank flank tongue ribeye. Beef pork chop sirloin venison chicken jowl.",
      "Doner corned beef kielbasa beef ribs ground round cow salami swine."
    ]
  }
];

              
            
!
999px

Console