Progressive enhancement
Now that we’ve seen how semantic HTML alone produces a fully-responsive web page, where do we go next? Our plain HTML page is ugly as sin, so we’re gonna need some CSS to spiff it up. But today let’s keep the CSS simple.
Start with the HTML
To begin, let’s start with some essential HTML:
This example shows a details element with CSS class xyz-accordion (substitute your own namespace for the “xyz”). The details element contains a summary element with class xyz-accordion-summary and a div element with class xyz-accordion-content. The div element contains two paragraphs of text. This text will be hidden until the details block is opened.
<details class="xyz-accordion">
<summary class="xyz-accordion-summary">Always visible</summary>
<div class="xyz-accordion-content">
<p>Hidden content</p>
<p>More hidden content</p>
</div>
</details>
<details> element makes a decent accordion component.Try the HTML-only accordion.
No doubt you have spotted the class attributes. We prefer to apply CSS by class. We can fall back to class-and-tag-name if we need more specificity, as you’ll see below.
So what’s up with the “xyz-” prefix?
Simple. To avoid name clashes with imported CSS, we namespace our classes. For example, the Craft Code site might use “cc-” as in cc-accordion. Of course, if your site is a simple one, that might be overkill.
Add the initial CSS
Let’s add a bit of CSS to give it that “accordion” look and feel. Here we go:
This example shows our initial CSS for our accordion component.
To the details element we apply via the CSS class values for the properties background-color, border-radius, border, and color.
To the summary element we apply via the CSS class values for the properties background-color, color, font-size, margin, and padding.
Finally, to the div element containing the content we apply via the CSS class values for the properties border-top and padding.
.xyz-accordion {
background-color: #fafafa;
border-radius: 5px;
border: 1px solid #747481;
color: #2c2c30;
}
.xyz-accordion-summary {
background-color: #0d4872;
color: #fafafa;
font-size: 1.625rem;
margin: 0;
padding: 1rem;
}
.xyz-accordion-summary:focus-visible {
outline: 2px solid #fafafa;
outline-offset: -6px;
}
.xyz-accordion-content {
border-top: 1px solid #747481;
padding: 0 1rem;
}
<details> element like an accordion.Try the accordion with basic CSS.
This is already a workable accordion component. But most accordions have more than one expandable element. So let’s add a few more elements:
This example expands on our basic HTML example above. We wrap our single accordion component in a div element. Then we expand that to three individual accordion components to create a full accordion.
We give the outer div element the CSS class xyz-accordion-group. In this example, the three inner details elements contain bios for three famous mathematicians.
<div class="xyz-accordion-group">
<details class="xyz-accordion">
<summary class="xyz-accordion-summary">
Katherine Johnson
</summary>
<div class="xyz-accordion-content">
<p>
<a
href="https://en.wikipedia.org/wiki/Katherine_Johnson"
rel="external"
>Creola Katherine Johnson</a
>
was an American mathematician whose calculations ....
</p>
</div>
</details>
<details class="xyz-accordion">
<summary class="xyz-accordion-summary">
Dorothy Vaughan
</summary>
<div class="xyz-accordion-content">
<p>
<a
href="https://en.wikipedia.org/wiki/Dorothy_Vaughan"
rel="external"
>Dorothy Jean Johnson Vaughan</a
>
was an American mathematician and human computer who ....
</p>
</div>
</details>
<details class="xyz-accordion">
<summary class="xyz-accordion-summary">Mary Jackson</summary>
<div class="xyz-accordion-content">
<p>
<a
href="https://en.wikipedia.org/wiki/Mary_Jackson_(engineer)"
rel="external"
>Mary Jackson</a
>
was an American mathematician and aerospace engineer at ....
</p>
</div>
</details>
</div>
We have also adjusted the CSS a bit to make it work with multiple elements:
In this example, we create a CSS class for xyz-accordion-group and provide values for three properties: border-radius (5px), border, and padding (0).
Our CSS for our details elements (CSS class .xyz-accordion) is reduced to background-color and color.
Then we enhance our summary elements by setting background-color, border-bottom (removing border-top from the content div), color, font-size, margin (0), and padding (1rem).
We remove the border-bottom from the xyz-accordion:last-child summary element. We give the content div elements 1rem of padding on each side.
.xyz-accordion-group {
border-radius: 5px;
border: 1px solid #0d4872;
padding: 0;
}
.xyz-accordion {
background-color: #fafafa;
color: #2c2c30;
}
.xyz-accordion-summary {
background-color: #0d4872;
border-bottom: 1px solid #fafafa;
color: #fafafa;
font-size: 1.625rem;
margin: 0;
padding: 1rem;
}
.xyz-accordion-summary:focus-visible {
outline: 2px solid #fafafa;
outline-offset: -6px;
}
.xyz-accordion:last-child summary {
border-bottom: none;
}
.xyz-accordion-content {
padding: 0 1rem;
}
We added the
.xyz-accordion-groupproperties. And moved the outer border from the individual elements to the group. We also changed the border color to match the<summary>background color.We removed the top border from the content block. Then we replaced it with a bottom border on the
<summary>element. We also changed the color to the background color of the<details>elements. This makes it appear as a horizontal rule between the accordion elements.We don’t want that bottom border on the last element in the accordion group. So we used
.xyz-accordion:last-child summaryto remove that one bottom border.
Here is the full accordion example.
Enhance the accordion
Rats! The accordion elements open and close instantly when you click on the summary. It would be nice if we animated the open and close.
The browser already knows how to open and close these panels. All we’re adding is a little motion. CSS gives us ::details-content, a pseudo-element that wraps the part of each <details> that expands and collapses.
@supports selector(details::details-content)
and (interpolate-size: allow-keywords)
and (transition-behavior: allow-discrete) {
@media (prefers-reduced-motion: no-preference) {
.xyz-accordion {
interpolate-size: allow-keywords;
}
.xyz-accordion::details-content {
block-size: 0;
overflow: clip;
transition:
block-size 250ms ease-in-out,
content-visibility 250ms allow-discrete;
}
.xyz-accordion[open]::details-content {
block-size: auto;
}
}
}
The panel goes from a block size of 0 to auto. By setting interpolate-size: allow-keywords on the accordion, we let the browser animate to the content’s natural size. It can still grow if the text size or viewport changes.
The second transition keeps the content visible until the panel finishes closing. allow-discrete lets content-visibility switch between visible and hidden during the transition, and overflow: clip keeps the contents inside the panel as its size changes.
The final version is here.
Try opening and closing a panel with the keyboard. Then turn off JavaScript and try again. The animation is CSS, and the browser handles the toggle. If the browser lacks any of the CSS features we’re using, or the reader prefers reduced motion, the panels open and close instantly.
It even works on the Lynx text-only browser, (external site) which doesn’t recognize the <details> element. All that means is that the accordion is always open.
That’s progressive enhancement for you. The HTML gives us a working disclosure. The CSS adds motion where the browser and the reader allow it.