Interactive HTML5 Elements and Global Attributes: details, summary, dialog, template, data-*, hidden, contenteditable and More
details and summary: An Accordion
html
<details>
<summary>What is your refund policy?</summary>
<p>You can ask for a refund within 30 days.</p>
</details>
<details open>
<summary>Do you ship worldwide?</summary>
<p>Yes, to most countries.</p>
</details>
No JavaScript. The browser opens and closes it and the keyboard works. Add open to start opened.
dialog: A Popup Window
html
<button id="openBtn">Open help</button>
<dialog id="helpDialog">
<h2>Help</h2>
<p>Press Esc to close this window.</p>
<form method="dialog">
<button>Close</button>
</form>
</dialog>
<script>
const dlg = document.getElementById("helpDialog");
document.getElementById("openBtn").addEventListener("click", () => dlg.showModal());
</script>
showModal()opens it above the page and traps focus inside- Esc closes it
- A form with
method="dialog"closes the dialog when its button is pressed - Style the dim background with
dialog::backdrop
template: Hidden Markup to Copy
html
<template id="card">
<article class="card">
<h3></h3>
<p></p>
</article>
</template>
<div id="list"></div>
<script>
const tpl = document.getElementById("card");
const copy = tpl.content.cloneNode(true);
copy.querySelector("h3").textContent = "New card";
copy.querySelector("p").textContent = "Made from a template.";
document.getElementById("list").appendChild(copy);
</script>
Content inside template is not shown until JavaScript copies it into the page.
Global Attributes
Global attributes can be used on almost any element.
| Attribute | Use |
|---|---|
id | Unique name. Also used for bookmarks |
class | Reusable names for CSS |
data-* | Your own data, such as data-price="20", read with JavaScript |
hidden | Hide the element |
contenteditable | Let the user edit the text |
draggable | Allow drag and drop |
tabindex | Control keyboard focus order. Use 0 or -1 only |
title | Tooltip text, not reliable for important information |
lang | Language of the element |
translate | Mark text that should not be translated |
html
<button class="buy" data-product-id="42" data-price="20">Buy</button>
<p contenteditable="true">Click and type here.</p>
<p hidden>You cannot see me.</p>
tabindex Do not use a positive
tabindex such as 5. It breaks the natural order. Prefer real buttons and links.
Practice
- Create an FAQ with three details blocks
- Open a dialog from a button and close it with the form
- Store a price in
data-priceand read it in the console
Conclusion
details, dialog and template give you accordions, popups and reusable markup with little code. Global attributes work on any element.
Quick Summary: details; dialog; template; global attributes.

