Expandable checkbox tree in HTML, CSS and vanilla JS
A copy-paste Expandable checkbox tree in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="example-checkbox-tree" data-dcl-scheme="light" aria-labelledby="example-checkbox-tree-title">
<header><h2 id="example-checkbox-tree-title">Expandable checkbox tree</h2>
<p>Nested options stay visible in a tree whose branches can be expanded or collapsed independently.</p></header>
<div class="parts">
<fieldset>
<legend>Library access</legend>
<div class="tree">
<div class="tree-node" data-node>
<label class="choice"><input type="checkbox" data-parent><span><strong>Design files</strong></span></label>
<details open><summary>Design categories</summary><div class="tree-children">
<label class="choice"><input type="checkbox" checked data-child><span>Web templates</span></label>
<details><summary>Print files</summary><div class="tree-children">
<label class="choice"><input type="checkbox" data-child><span>Posters</span></label>
<label class="choice"><input type="checkbox" data-child><span>Reports</span></label>
</div></details>
</div></details>
</div>
<div class="tree-node" data-node>
<label class="choice"><input type="checkbox" data-parent><span><strong>Engineering files</strong></span></label>
<details><summary>Engineering categories</summary><div class="tree-children">
<label class="choice"><input type="checkbox" data-child><span>API examples</span></label>
<label class="choice"><input type="checkbox" data-child><span>Architecture notes</span></label>
</div></details>
</div>
</div>
</fieldset>
<p class="status" role="status" aria-live="polite" data-count></p>
</div>
<script>
(() => {
const root = document.currentScript.closest(".example-checkbox-tree");
const instance = Array.from(document.getElementsByClassName("example-checkbox-tree")).indexOf(root);
if (instance > 0) {
const singleReferences = ["id", "aria-activedescendant", "for", "list", "name"];
const tokenReferences = ["aria-labelledby", "aria-controls", "aria-describedby"];
for (const element of [root, ...root.querySelectorAll("*")]) {
for (const attr of singleReferences) {
if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr) + "--" + instance);
}
for (const attr of tokenReferences) {
if (element.hasAttribute(attr)) {
const rewritten = element.getAttribute(attr).trim().split(/\s+/).filter(Boolean).map((reference) => reference + "--" + instance);
element.setAttribute(attr, rewritten.join(" "));
}
}
const href = element.getAttribute("href");
if (href && href.startsWith("#")) element.setAttribute("href", href + "--" + instance);
}
}
const preference = matchMedia("(prefers-color-scheme: dark)");
const apply = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
preference.addEventListener("change", apply);
apply();
const nodes = Array.from(root.querySelectorAll("[data-node]"));
const children = Array.from(root.querySelectorAll("[data-child]"));
const count = root.querySelector("[data-count]");
function syncNode(node) {
const parent = node.querySelector("[data-parent]");
const items = Array.from(node.querySelectorAll("[data-child]"));
const selected = items.filter((input) => input.checked).length;
parent.checked = selected === items.length && items.length > 0;
parent.indeterminate = selected > 0 && selected < items.length;
}
function syncAll() {
nodes.forEach(syncNode);
const selected = children.filter((input) => input.checked).length;
count.textContent = selected + " of " + children.length + " files selected.";
}
for (const node of nodes) {
const parent = node.querySelector("[data-parent]");
const items = Array.from(node.querySelectorAll("[data-child]"));
parent.addEventListener("change", () => {
items.forEach((input) => { input.checked = parent.checked; });
syncAll();
});
}
children.forEach((input) => input.addEventListener("change", syncAll));
syncAll();
})();
</script></section>
<style>
.example-checkbox-tree { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-checkbox-tree a { color: #005c62; }
.example-checkbox-tree button, .example-checkbox-tree summary, .example-checkbox-tree input, .example-checkbox-tree select, .example-checkbox-tree textarea {
color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-checkbox-tree button:hover, .example-checkbox-tree summary:hover, .example-checkbox-tree button[aria-selected="true"], .example-checkbox-tree button[aria-pressed="true"] {
color: #ffffff; background-color: #00535a;
}
.example-checkbox-tree dialog { color: #11292f; background-color: #ffffff; }
.example-checkbox-tree [data-note] { color: #11292f; background-color: #e4edef; }
.example-checkbox-tree [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-checkbox-tree [data-success] { color: #14532d; background-color: #dcfce7; }
.example-checkbox-tree [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-checkbox-tree :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.example-checkbox-tree[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-checkbox-tree[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-checkbox-tree[data-dcl-scheme="dark"] button, .example-checkbox-tree[data-dcl-scheme="dark"] summary,
.example-checkbox-tree[data-dcl-scheme="dark"] input, .example-checkbox-tree[data-dcl-scheme="dark"] select, .example-checkbox-tree[data-dcl-scheme="dark"] textarea {
color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-checkbox-tree[data-dcl-scheme="dark"] button:hover, .example-checkbox-tree[data-dcl-scheme="dark"] summary:hover,
.example-checkbox-tree[data-dcl-scheme="dark"] button[aria-selected="true"], .example-checkbox-tree[data-dcl-scheme="dark"] button[aria-pressed="true"] {
color: #11292f; background-color: #8fe1da;
}
.example-checkbox-tree[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-checkbox-tree[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-checkbox-tree[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-checkbox-tree[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-checkbox-tree[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-checkbox-tree[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.example-checkbox-tree, .example-checkbox-tree * { box-sizing: border-box; min-width: 0; }
.example-checkbox-tree { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-checkbox-tree h2, .example-checkbox-tree h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-checkbox-tree header p, .example-checkbox-tree p { margin-block: 0.5rem; }
.example-checkbox-tree ul, .example-checkbox-tree ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-checkbox-tree dl { margin-block: 0.5rem; }
.example-checkbox-tree dt { font-weight: 700; }
.example-checkbox-tree dd { margin: 0 0 0.6rem; }
.example-checkbox-tree fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-checkbox-tree legend { padding-inline: 0.25rem; font-weight: 700; }
.example-checkbox-tree button, .example-checkbox-tree input, .example-checkbox-tree select, .example-checkbox-tree textarea {
min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-checkbox-tree input, .example-checkbox-tree select, .example-checkbox-tree textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-checkbox-tree input[type="checkbox"], .example-checkbox-tree input[type="radio"] {
display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-checkbox-tree input[type="file"] { padding: 0.45rem; }
.example-checkbox-tree input[type="range"] { padding-inline: 0; }
.example-checkbox-tree textarea { min-block-size: 6rem; resize: vertical; }
.example-checkbox-tree button, .example-checkbox-tree summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-checkbox-tree button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-checkbox-tree table { border-collapse: collapse; inline-size: 100%; }
.example-checkbox-tree th, .example-checkbox-tree td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-checkbox-tree [hidden] { display: none !important; }
.example-checkbox-tree [data-note], .example-checkbox-tree [data-error], .example-checkbox-tree [data-success], .example-checkbox-tree [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-checkbox-tree .parts { display: grid; gap: 1rem; }
.example-checkbox-tree .field, .example-checkbox-tree .fields { display: grid; gap: 0.4rem; }
.example-checkbox-tree .fields { gap: 0.75rem; }
.example-checkbox-tree .actions, .example-checkbox-tree .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-checkbox-tree .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-checkbox-tree .plain-list { margin: 0; padding: 0; list-style: none; }
.example-checkbox-tree .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-checkbox-tree .choice input { margin-block-start: 0.25rem; }
.example-checkbox-tree .choice-stack { display: grid; gap: 0.55rem; }
.example-checkbox-tree .tile { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0.55rem; align-items: start; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-checkbox-tree .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-checkbox-tree .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-checkbox-tree .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-checkbox-tree .hint { font-size: 0.875rem; }
.example-checkbox-tree .status { min-block-size: 1.5rem; margin-block: 0; }
.example-checkbox-tree .two-column, .example-checkbox-tree .split-layout { display: grid; gap: 1rem; }
.example-checkbox-tree .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-checkbox-tree .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-checkbox-tree .visually-hidden {
position: absolute; inline-size: 1px; block-size: 1px; padding: 0; margin: -1px;
overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}
@media (min-width: 34rem) {
.example-checkbox-tree .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.example-checkbox-tree .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
.example-checkbox-tree { padding: 0.75rem; }
.example-checkbox-tree .actions, .example-checkbox-tree .inline-row { align-items: stretch; }
.example-checkbox-tree .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-checkbox-tree * { transition: none !important; } }
.example-checkbox-tree .tree, .example-checkbox-tree .tree-children { display: grid; gap: 0.55rem; }
.example-checkbox-tree .tree-children { margin-inline-start: 1.4rem; padding-block: 0.45rem; }
.example-checkbox-tree summary { padding-block: 0.2rem; }
</style>
When to use
Use this for hierarchical permissions or category selection where people need to see parent-child structure. Keep the tree shallow; deep administration trees need search as well as disclosure controls.
Avoid
Do not represent independent flat choices as a tree; the extra hierarchy and indeterminate states only add work.
Accessibility
Expansion uses native details and summary controls, while every checkbox remains a native labelled input. Parent controls expose indeterminate state and the total count is written in text. Main text contrast is 15.19:1 light and 13.07:1 dark; controls are 12.78:1 and 10.94:1.
Was this useful? Send feedback.