Error summary grouped by section in HTML, CSS and vanilla JS
A copy-paste Error summary grouped by section in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="example-error-summary-sections" data-dcl-scheme="light" aria-labelledby="example-error-summary-sections-title">
<header><h2 id="example-error-summary-sections-title">Error summary grouped by section</h2>
<p>Errors are grouped under Contact and Delivery headings so long forms keep their structure.</p></header>
<div class="parts">
<form data-form novalidate>
<div class="summary" tabindex="-1" role="alert" data-summary data-error hidden><h3>There is a problem</h3><div data-groups></div></div>
<fieldset><legend>Contact</legend><div class="two-column">
<div class="field"><label for="example-error-summary-sections-name">Name</label><input type="text" id="example-error-summary-sections-name" autocomplete="name" required data-section="Contact"></div>
<div class="field"><label for="example-error-summary-sections-email">Email</label><input type="email" id="example-error-summary-sections-email" autocomplete="email" required data-section="Contact"></div>
</div></fieldset>
<fieldset><legend>Delivery</legend><div class="two-column">
<div class="field"><label for="example-error-summary-sections-date">Delivery date</label><input type="date" id="example-error-summary-sections-date" required data-section="Delivery"></div>
<div class="field"><label for="example-error-summary-sections-slot">Delivery slot</label><select id="example-error-summary-sections-slot" required data-section="Delivery"><option value="">Choose a slot</option><option>Morning</option><option>Afternoon</option></select></div>
</div></fieldset>
<div class="actions"><button type="submit">Continue</button></div>
</form>
</div>
<script>
(() => {
const root = document.currentScript.closest(".example-error-summary-sections");
const instance = Array.from(document.getElementsByClassName("example-error-summary-sections")).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 form = root.querySelector("[data-form]");
const summary = root.querySelector("[data-summary]");
const groupsBox = root.querySelector("[data-groups]");
const inputs = Array.from(root.querySelectorAll("input, select"));
function submitForm() {
groupsBox.replaceChildren();
const invalid = inputs.filter((input) => !input.checkValidity());
summary.hidden = invalid.length === 0;
if (!invalid.length) return;
const bySection = new Map();
for (const input of invalid) {
input.setAttribute("aria-invalid", "true");
if (!bySection.has(input.dataset.section)) bySection.set(input.dataset.section, []);
bySection.get(input.dataset.section).push(input);
}
for (const [section, items] of bySection) {
const heading = document.createElement("h4");
const list = document.createElement("ul");
heading.textContent = section;
for (const input of items) {
const item = document.createElement("li");
const link = document.createElement("a");
link.href = "#" + input.id;
link.textContent = input.labels[0].textContent + " needs attention.";
link.addEventListener("click", (click) => { click.preventDefault(); input.focus(); });
item.append(link);
list.append(item);
}
groupsBox.append(heading, list);
}
summary.focus();
}
form.addEventListener("submit", (event) => {
event.preventDefault();
submitForm();
});
const submitButton = form.querySelector('button[type="submit"]');
if (submitButton) {
submitButton.addEventListener("click", (event) => {
event.preventDefault();
submitForm();
});
}
form.addEventListener("keydown", (event) => {
if (event.key !== "Enter" || event.defaultPrevented) return;
if (!event.target.matches("input, select")) return;
event.preventDefault();
submitForm();
});
form.addEventListener("input", (event) => {
if (event.target.matches("input, select")) event.target.setAttribute("aria-invalid", String(!event.target.checkValidity()));
});
})();
</script></section>
<style>
.example-error-summary-sections { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-error-summary-sections a { color: #005c62; }
.example-error-summary-sections button, .example-error-summary-sections summary, .example-error-summary-sections input, .example-error-summary-sections select, .example-error-summary-sections textarea {
color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-error-summary-sections button:hover, .example-error-summary-sections summary:hover, .example-error-summary-sections button[aria-selected="true"], .example-error-summary-sections button[aria-pressed="true"] {
color: #ffffff; background-color: #00535a;
}
.example-error-summary-sections dialog { color: #11292f; background-color: #ffffff; }
.example-error-summary-sections [data-note] { color: #11292f; background-color: #e4edef; }
.example-error-summary-sections [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-error-summary-sections [data-success] { color: #14532d; background-color: #dcfce7; }
.example-error-summary-sections [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-error-summary-sections :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.example-error-summary-sections[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-error-summary-sections[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-error-summary-sections[data-dcl-scheme="dark"] button, .example-error-summary-sections[data-dcl-scheme="dark"] summary,
.example-error-summary-sections[data-dcl-scheme="dark"] input, .example-error-summary-sections[data-dcl-scheme="dark"] select, .example-error-summary-sections[data-dcl-scheme="dark"] textarea {
color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-error-summary-sections[data-dcl-scheme="dark"] button:hover, .example-error-summary-sections[data-dcl-scheme="dark"] summary:hover,
.example-error-summary-sections[data-dcl-scheme="dark"] button[aria-selected="true"], .example-error-summary-sections[data-dcl-scheme="dark"] button[aria-pressed="true"] {
color: #11292f; background-color: #8fe1da;
}
.example-error-summary-sections[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-error-summary-sections[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-error-summary-sections[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-error-summary-sections[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-error-summary-sections[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-error-summary-sections[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.example-error-summary-sections, .example-error-summary-sections * { box-sizing: border-box; min-width: 0; }
.example-error-summary-sections { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-error-summary-sections h2, .example-error-summary-sections h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-error-summary-sections header p, .example-error-summary-sections p { margin-block: 0.5rem; }
.example-error-summary-sections ul, .example-error-summary-sections ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-error-summary-sections dl { margin-block: 0.5rem; }
.example-error-summary-sections dt { font-weight: 700; }
.example-error-summary-sections dd { margin: 0 0 0.6rem; }
.example-error-summary-sections fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-error-summary-sections legend { padding-inline: 0.25rem; font-weight: 700; }
.example-error-summary-sections button, .example-error-summary-sections input, .example-error-summary-sections select, .example-error-summary-sections textarea {
min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-error-summary-sections input, .example-error-summary-sections select, .example-error-summary-sections textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-error-summary-sections input[type="checkbox"], .example-error-summary-sections input[type="radio"] {
display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-error-summary-sections input[type="file"] { padding: 0.45rem; }
.example-error-summary-sections input[type="range"] { padding-inline: 0; }
.example-error-summary-sections textarea { min-block-size: 6rem; resize: vertical; }
.example-error-summary-sections button, .example-error-summary-sections summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-error-summary-sections button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-error-summary-sections table { border-collapse: collapse; inline-size: 100%; }
.example-error-summary-sections th, .example-error-summary-sections td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-error-summary-sections [hidden] { display: none !important; }
.example-error-summary-sections [data-note], .example-error-summary-sections [data-error], .example-error-summary-sections [data-success], .example-error-summary-sections [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-error-summary-sections .parts { display: grid; gap: 1rem; }
.example-error-summary-sections .field, .example-error-summary-sections .fields { display: grid; gap: 0.4rem; }
.example-error-summary-sections .fields { gap: 0.75rem; }
.example-error-summary-sections .actions, .example-error-summary-sections .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-error-summary-sections .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-error-summary-sections .plain-list { margin: 0; padding: 0; list-style: none; }
.example-error-summary-sections .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-error-summary-sections .choice input { margin-block-start: 0.25rem; }
.example-error-summary-sections .choice-stack { display: grid; gap: 0.55rem; }
.example-error-summary-sections .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-error-summary-sections .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-error-summary-sections .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-error-summary-sections .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-error-summary-sections .hint { font-size: 0.875rem; }
.example-error-summary-sections .status { min-block-size: 1.5rem; margin-block: 0; }
.example-error-summary-sections .two-column, .example-error-summary-sections .split-layout { display: grid; gap: 1rem; }
.example-error-summary-sections .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-error-summary-sections .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-error-summary-sections .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-error-summary-sections .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.example-error-summary-sections .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
.example-error-summary-sections { padding: 0.75rem; }
.example-error-summary-sections .actions, .example-error-summary-sections .inline-row { align-items: stretch; }
.example-error-summary-sections .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-error-summary-sections * { transition: none !important; } }
.example-error-summary-sections .summary[hidden] { display: none !important; }
.example-error-summary-sections form, .example-error-summary-sections .summary { display: grid; gap: 0.75rem; }
.example-error-summary-sections h4 { margin-block: 0.5rem 0; }
</style>
When to use
Use this when a form already has meaningful sections and people need to repair errors in context. Keep group headings in the summary rather than flattening unrelated messages together.
Avoid
Do not invent group names that differ from the visible fieldset legends; matching wording is what lets people orient themselves.
Accessibility
Every error link focuses a native labelled field, the alert container receives focus after submit, and section names are text rather than visual indentation alone. Main text contrast is 15.19:1 light and 13.07:1 dark; error text is 8.2:1 light and 10.25:1 dark.
Was this useful? Send feedback.