HTML Advanced: Accessible, Findable, Fast Pages
Interactive HTML Elements
In this lesson you will
- Build an accordion with details and summary
- Open and close a modal with the dialog element
- Show progress and measurements with progress and meter
Introduction
Accordions, pop-up windows and progress bars used to need lots of JavaScript. Modern HTML has built-in elements for them. They are keyboard friendly and accessible from the start, so they are usually a better choice than building your own.
Details and summary
details creates a section that opens and closes. summary is the clickable heading. Add the open attribute to start it open.
Click the headings. You can also focus them with Tab and open them with Enter or Space. No JavaScript at all.
Quick quiz
Which attribute makes a details element start open?
Progress and meter
progressshows how far a task has got, such as an upload.metershows a value within a known range, such as disk space used.
Use progress for something that is going to finish, and meter for a gauge.
The dialog element
dialog is a window that appears above the page. Opening it with showModal() dims the rest of the page, traps keyboard focus inside, and lets the Escape key close it. That needs a tiny bit of JavaScript to open.
A form with method="dialog" closes the dialog when its button is clicked. Try it, and also try pressing Escape.
Quick quiz
Which method opens a dialog as a modal?
Try it yourself
Add a second details item to the page below, and change the progress value to 90.
Summary
detailsandsummarymake an accordion with no JavaScript.progressandmetershow progress and gauges.dialogwithshowModal()gives you an accessible modal window.- Prefer built-in elements before building your own widgets.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading