Skip to content
TheChatArchiveThe Chat Archive
GPT 5.1· openaiHugging Face Frontier Reasoning Traces

Create a CSS-based accordion with smooth animations and no JavaScript.

  1. User

    Create a CSS-based accordion with smooth animations and no JavaScript.

  2. Assistant

    Crafting HTML and CSS for an Accordion

    I'm working on structuring an accordion component with HTML and CSS. The HTML portion includes a checkbox for toggle functionality, while the CSS handles the display and animations, like max height and opacity for the content. Animating padding is tricky since we ideally want consistent spacing when closed. I’m considering adjustments to manage overflow and define transitions efficiently. Lastly, I want to incorporate icon rotation when sections are opened. This will bring everything to life!Creating a Pure CSS Accordion

    I’m focusing on a CSS-only accordion without using JavaScript, which keeps it simple. To meet the user’s needs, I’ll provide a complete, copy-paste-ready snippet that includes the full HTML structure and a tag. I aim to include an overview as well as the code, organized clearly. The explanation will detail how checkboxes and labels are used to create the toggling effect. I’ll avoid any generic phrases to keep the tone direct and informative. Let’s get started! Here’s a copy‑paste‑ready accordion that uses only HTML + CSS, with smooth open/close animations and no JavaScript.


    Full Example (HTML + CSS)

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <title>Pure CSS Accordion</title>
      <style>
        /* Base styles */
        body {
          font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
          background: #f4f4f6;
          padding: 40px;
        }
    
        .accordion {
          max-width: 600px;
          margin: 0 auto;
          border-radius: 8px;
          background: #fff;
          box-shadow:
            0 12px 30px rgba(15, 23, 42, 0.08),
            0 1px 4px rgba(15, 23, 42, 0.06);
          overflow: hidden;
        }
    
        .accordion-item + .accordion-item {
          border-top: 1px solid #e5e7eb;
        }
    
        /* Hide the checkbox inputs */
        .accordion-item input {
          display: none;
        }
    
        /* Header (clickable label) */
        .accordion-header {
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 1rem 1.25rem;
          cursor: pointer;
          background: #fff;
          transition: background 0.25s ease;
          user-select: none;
        }
    
        .accordion-header:hover {
          background: #f9fafb;
        }
    
        .accordion-title {
          font-size: 1rem;
          font-weight: 600;
          color: #111827;
        }
    
        /* Icon */
        .accordion-icon {
          width: 1.5rem;
          height: 1.5rem;
          border-radius: 999px;
          border: 1px solid #d1d5db;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          font-size: 0.9rem;
          color: #6b7280;
          transition:
            transform 0.25s ease,
            background 0.25s ease,
            color 0.25s ease,
            border-color 0.25s ease;
        }
    
        /* Content wrapper (animated) */
        .accordion-content {
          max-height: 0;
          overflow: hidden;
          opacity: 0;
          transform: translateY(-4px);
          transition:
            max-height 0.35s ease,
            opacity 0.35s ease,
            transform 0.35s ease;
          background: #f9fafb;
        }
    
        /* Actual content padding is inside to avoid jumpiness */
        .accordion-content-inner {
          padding: 0 1.25rem 1rem;
          color: #4b5563;
          line-height: 1.5;
          font-size: 0.95rem;
        }
    
        /* When checkbox is checked, expand the content */
        .accordion-item input:checked ~ .accordion-content {
          max-height: 400px; /* big enough for your content */
          opacity: 1;
          transform: translateY(0);
        }
    
        /* Rotate and restyle icon on open */
        .accordion-item input:checked + label .accordion-icon {
          transform: rotate(90deg);
          background: #111827;
          color: #f9fafb;
          border-color: #111827;
        }
      </style>
    </head>
    <body>
    
      <div class="accordion">
        <!-- Item 1 -->
        <div class="accordion-item">
          <input type="checkbox" id="acc1">
          <label class="accordion-header" for="acc1">
            <span class="accordion-title">What is this accordion?</span>
            <span class="accordion-icon">›</span>
          </label>
          <div class="accordion-content">
            <div class="accordion-content-inner">
              <p>
                This is a pure CSS accordion. It uses a hidden checkbox to track the
                open/closed state. The content’s <code>max-height</code> and opacity
                are animated with CSS transitions to create a smooth effect.
              </p>
            </div>
          </div>
        </div>
    
        <!-- Item 2 -->
        <div class="accordion-item">
          <input type="checkbox" id="acc2">
          <label class="accordion-header" for="acc2">
            <span class="accordion-title">Can I open multiple sections?</span>
            <span class="accordion-icon">›</span>
          </label>
          <div class="accordion-content">
            <div class="accordion-content-inner">
              <p>
                Yes. Because each section uses its own checkbox, multiple sections
                can be open at the same time. If you want only one open at once,
                use <code>type="radio"</code> with the same <code>name</code>.
              </p>
            </div>
          </div>
        </div>
    
        <!-- Item 3 -->
        <div class="accordion-item">
          <input type="checkbox" id="acc3">
          <label class="accordion-header" for="acc3">
            <span class="accordion-title">How does the animation work?</span>
            <span class="accordion-icon">›</span>
          </label>
          <div class="accordion-content">
            <div class="accordion-content-inner">
              <p>
                The hidden checkbox toggles the <code>:checked</code> selector.
                When checked, we increase <code>max-height</code> (to a large value),
                and animate:
              </p>
              <ul>
                <li><code>max-height</code> for the sliding effect</li>
                <li><code>opacity</code> for fade-in</li>
                <li><code>transform: translateY()</code> for a slight motion</li>
              </ul>
              <p>
                No JavaScript is required—only HTML and CSS.
              </p>
            </div>
          </div>
        </div>
      </div>
    
    </body>
    </html>
    

    Key ideas

    • Each accordion item has a hidden <input type="checkbox">.
    • The <label> with for="id" toggles that checkbox.
    • CSS uses input:checked ~ .accordion-content to:
      • expand via max-height
      • fade/move via opacity and transform
    • The icon rotates with input:checked + label .accordion-icon.

    You can adjust max-height, transition durations, and colors to fit your design.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.