I've started with the fundamental idea: an accordion built entirely with CSS. The goal is clear – a functional and smoothly animated accordion. The key constraints are no JavaScript and using only CSS. Now, I'm digging into the core mechanism to make it work.
Selecting the Method
After evaluating both the checkbox/radio hack and the details/summary approach, I'm leaning toward the former for the main solution. It allows greater flexibility in styling. I'll include the other approach as a bonus, but the main focus will be on the checkbox/radio method, refining the CSS selectors and animations.
Refining the Animation
I'm now revisiting the animation challenge, specifically how to avoid the choppy max-height transitions. The grid trick is very interesting, so I will likely incorporate it. After further consideration, I will probably implement the height transition using a combination of the checkbox hack, a grid for the content area, and grid-template-rows for the smooth expansion. This will be the main approach.
Confirming Implementation Details
The core structure is now clearly defined. It will be the radio input hack, ensuring only one panel opens at a time. The HTML structure is finalized, including the necessary classes and IDs. I'm focusing on the grid trick for the smooth animation, which involves setting the grid template rows to 0fr by default and then animating it to 1fr on click.
Evaluating the HTML Structure
I'm now revisiting the HTML structure. The radio input, label, and content structure is solidified. Thinking about best practices, and the balance of semantic and stylistic needs, this structure should be the foundation. I will finalize and refine the HTML markup before beginning to write the CSS.
Confirming the Structure
Okay, the approach is solidified. The checkbox hack, using radio buttons, is the core. I'll utilize the grid trick for seamless animations, employing grid-template-rows to smoothly manage content expansion. I'm now certain this is the ideal solution.
Here is a complete, modern solution for a CSS-only accordion.
This approach uses the Checkbox/Radio Hack combined with the modern CSS Grid state transition (grid-template-rows). This is currently the best way to achieve a smooth height animation from 0 to auto without JavaScript.
The Code
1. HTML Structure
We use <input type="radio"> so only one panel stays open at a time. Change them to type="checkbox" if you want multiple panels open simultaneously.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Only Accordion</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="accordion">
<!-- Item 1 -->
<div class="accordion-item">
<!-- Hidden Input -->
<input type="radio" name="accordion" id="item-1" checked>
<!-- Clickable Label -->
<label class="accordion-title" for="item-1">
What is a CSS-only accordion?
<span class="icon"></span>
</label>
<!-- Content Wrapper -->
<div class="accordion-content">
<div class="inner-content">
<p>A CSS-only accordion uses standard HTML form elements (like radios or checkboxes) to track the "state" of the UI. By using the <code>:checked</code> pseudo-class, we can style the sibling elements to expand or collapse.</p>
</div>
</div>
</div>
<!-- Item 2 -->
<div class="accordion-item">
<input type="radio" name="accordion" id="item-2">
<label class="accordion-title" for="item-2">
How does the animation work?
<span class="icon"></span>
</label>
<div class="accordion-content">
<div class="inner-content">
<p>Historically, you couldn't animate height to "auto". However, using modern CSS Grid, we can transition <code>grid-template-rows</code> from 0fr to 1fr. This allows the content to expand smoothly regardless of how much text is inside.</p>
</div>
</div>
</div>
<!-- Item 3 -->
<div class="accordion-item">
<input type="radio" name="accordion" id="item-3">
<label class="accordion-title" for="item-3">
Is it responsive?
<span class="icon"></span>
</label>
<div class="accordion-content">
<div class="inner-content">
<p>Yes! Because we aren't using fixed heights (pixels), the accordion adapts perfectly to mobile screens, tablets, and desktops. The content flows naturally within the container.</p>
</div>
</div>
</div>
</div>
</body>
</html>
2. CSS Styles
css
/* Basic Reset / Layout */
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
padding-top: 50px;
}
.accordion {
width: 100%;
max-width: 600px;
background: white;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0,0,0,0.1);
overflow: hidden;
}
/* --- The Logic --- */
/* Hide the radio inputs interaction but keep them logically present */
.accordion-item input {
position: absolute;
opacity: 0;
z-index: -1;
}
/* --- The Style & Animation --- */
.accordion-item {
border-bottom: 1px solid #eee;
}
/* The Clickable Header */
.accordion-title {
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
font-weight: 600;
color: #333;
cursor: pointer;
background-color: #fff;
transition: background-color 0.3s ease;
}
/* Hover effect */
.accordion-title:hover {
background-color: #f9f9f9;
}
/* Highlight title when active */
.accordion-item input:checked ~ .accordion-title {
color: #007bff;
}
/* The Content Wrapper (grid animation trick) */
.accordion-content {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 0.4s ease-out;
background-color: #fff;
}
/* The state when the input is checked */
.accordion-item input:checked ~ .accordion-content {
grid-template-rows: 1fr;
}
/* Inner container to handle padding/overflow */
.inner-content {
overflow: hidden;
}
/* Add padding inside the inner content, not the wrapper
(prevents padding from showing during animation) */
.inner-content p {
padding: 0 20px 20px 20px;
margin: 0;
color: #666;
line-height: 1.6;
}
/* --- The Icon Transformation --- */
.icon {
position: relative;
width: 12px;
height: 12px;
}
/* Use CSS pseudo-elements to create a '+' sign */
.icon::before,
.icon::after {
content: "";
position: absolute;
top: 50%;
left: 50%;
background-color: #333;
transition: transform 0.3s ease;
}
/* Horizontal line */
.icon::before {
width: 100%;
height: 2px;
transform: translate(-50%, -50%);
}
/* Vertical line */
.icon::after {
width: 2px;
height: 100%;
transform: translate(-50%, -50%);
}
/* Rotate into a minus or X when checked */
.accordion-item input:checked ~ .accordion-title .icon::after {
transform: translate(-50%, -50%) rotate(90deg);
/* Rotating the vertical line 90deg makes it horizontal,
leaving just a single horizontal line (minus sign) */
}
/* Optional: Rotate the whole icon for a chevron effect style instead */
/*
.accordion-item input:checked ~ .accordion-title .icon {
transform: rotate(45deg);
}
*/
How it works
State Management (No JS): We use input type="radio" paired with a <label>. Clicking the label triggers the hidden radio button.
Sibling Selectors: CSS uses the ~ or + selector. When input is checked, we find the sibling .accordion-content and change its style.
The Smooth Animation (Grid Trick):
Animating height from 0 to auto is impossible in standard CSS.
Previously, developers animated max-height (e.g., 0 to 1000px), but this caused timing issues (animation delay) if the content was short.
The Solution: We set the content to display: grid and animate grid-template-rows from 0fr (closed) to 1fr (open). This calculates the exact height of the content dynamically, resulting in a perfectly smooth slide.