Mailchimp: Grow sales with Customer Journey Smarts
We’ve mentioned before just how great the <details>
and <summary>
elements are. They’re great for quickly making accordions that are accessible to touch, mouse, and keyboard input:
<details>
and <summary>
can even be used to play/pause gifs! Click the pause button in the top right of this image to see it in action:
Neat, eh? But what else can these elements do? For a long while, I’ve only thought about details
as a way to make accordions. But the other day I started thinking about it a little bit, and the first thing I realized was maybe we could make better footnotes with them.
Here’s we typically make footnotes today with just HTML:
Clicking a link in a paragraph it pushes you to the bottom of the page where the description of the footnote is. And we do that by linking to the ID of elements back and forth. But! There are jQuery plugins like BigFoot out there that let you have footnotes inline.
Here’s that same example with the BigFoot script and jQuery applied to it:
Neat huh? BigFoot takes our accessible markup, removes all that link stuff, and throws it right inline as a little button and tooltip thing.
But what if we could use <details>
and <summary>
to replace this jQuery and plain ol’ HTML for our footnotes? Well, sadly, we can’t. If you try to place either of these elements within a <p>
tag, browsers render those elements outside the <p>
element:
Blah! This is annoying but I sorta get why it works like this. It would be great if we could replace the complexity of all these links and the crappy experience of hopping up and down the page. But we can’t.
Well, if we can’t do that, then what else can we use <details>
and <summary>
for? This had me thinking… what about tooltips? Well, yes! We can absolutely do that. Let’s say that we have some markup like this that expands the date shown:
Pretty standard, we’ve seen this before. But now let’s get to the styling which is the interesting bit. First up, we need to tackle the <details>
element:
This lets us absolutely position the information within our details element but it also lets the tooltip feel a bit more like a button on click with the background. Next, we can undo the default arrow for the summary
element by doing this:
And finally, we need to absolutely position the paragraph that we have within the details
element. And make a tiny little triangle that points up to the rest of the tooltip:
Which leads to something like this. Make sure to click the tooltip to see the description appear beneath it:
One nice thing we can do here to polish things up a tiny little bit is to add an animation when the tooltip is open:
And there you have it! If you click the tooltip it’ll pop open and if you tab and click space then it’ll open up, too.
But we can take this a little further. Something we probably don’t want is a bunch of these tooltips opening up all over the place. If we’re using tooltips heavily then that could get super clumsy and visually distracting. What we need to do is close the tooltip when we click outside of it. And we can do that with some light JavaScript:
Now when you click off the tooltip it’ll close it. Neato!
The <details>
and <summary>
elements can’t do everything but they can certainly do a lot of handy things — and I reckon that there’s a lot more this combination of elements can do. We just have to go find them. In fact, here are some more great posts on the topic.
Frontend Masters is the best place to get it. They have courses on all the most important front-end technologies, from React to CSS, from Vue to D3, and beyond with Node.js and Full Stack.
Frontend Masters is the best place to get it. They have courses on all the most important front-end technologies, from React to CSS, from Vue to D3, and beyond with Node.js and Full Stack.