6 UI parts that no longer need JavaScript
Native HTML handles accordions, modals, popovers, date pickers, color pickers, and form validation.
Published here · 4 sources · Resume reading
These six UI patterns used to require JavaScript libraries. Now they work with plain HTML.
1. Accordion (details and summary)
The details element expands and collapses content. Add the same name attribute to make only one open at a time [1] [2]:
<details name="faq">
<summary>Question one</summary>
<p>Answer one.</p>
</details>
<details name="faq">
<summary>Question two</summary>
<p>Answer two.</p>
</details>You should see clickable summaries that expand to show their content.
2. Modal dialog
The dialog element with showModal() creates a modal with a backdrop. For a declarative open, use a form with method="dialog" [4]:
<dialog id="confirm">
<p>Are you sure?</p>
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="confirm">Confirm</button>
</form>
</dialog>
<button onclick="document.getElementById('confirm').showModal()">Open</button>You should see a centered dialog with a dimmed backdrop when opened.
3. Popover (no modal)
The popover attribute creates a non-modal overlay. Use popovertarget on a button to toggle it without JavaScript [3]:
<button popovertarget="menu">Open menu</button>
<div id="menu" popover>
<p>Menu content</p>
</div>You should see the popover appear when clicked, and close when clicking outside.
4. Date picker
The date input type shows a native calendar picker on desktop and mobile:
<input type="date" name="start">You should see a date picker when the field is focused.
5. Color picker
The color input type opens a native color selector:
<input type="color" name="theme" value="#3b82f6">You should see a color swatch that opens a picker when clicked.
6. Form validation
Required fields, email format, and pattern matching work without JavaScript:
<form>
<input type="email" required placeholder="Email">
<input type="text" pattern="[0-9]{5}" placeholder="ZIP code">
<button type="submit">Submit</button>
</form>You should see browser validation messages when submitting invalid input.
Send this to the friend installing a JavaScript library just to build a modal or accordion.
Keep this guide
Filed under Dev tools
Questions about this guide
Do these work in all browsers?
Yes. The elements listed here have full support in Chrome, Firefox, Safari, and Edge as of 2024.
Can I style them?
Yes. Use CSS selectors like details[open], ::marker, and ::backdrop to customize appearance.
Sources
- MDN: details elementdeveloper.mozilla.org
- MDN blog: exclusive accordions using the HTML details elementdeveloper.mozilla.org
- web.dev: popover and dialogweb.dev
- MDN: dialog elementdeveloper.mozilla.org