Component Library

← Back to gallery

Live shrinking error summary in HTML, CSS and vanilla JS

A copy-paste Live shrinking error summary in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Forms · Tags: error summary, live validation, progress, inline errors, form

Live preview

Source (copy-ready)

<section class="example-error-summary-live-progress" data-dcl-scheme="light" aria-labelledby="example-error-summary-live-progress-title">
<header><h2 id="example-error-summary-live-progress-title">Live shrinking error summary</h2>
<p>After the first submit attempt, the summary shrinks immediately as each field is corrected.</p></header>
<div class="parts">
<form data-form novalidate>
  <div class="summary" role="alert" data-summary data-error hidden></div>
  <fieldset><legend>Delivery details</legend><div class="fields">
    <div class="field"><label for="example-error-summary-live-progress-name">Name</label><input type="text" id="example-error-summary-live-progress-name" autocomplete="name" aria-describedby="example-error-summary-live-progress-name-error" required data-name><p class="field-error" id="example-error-summary-live-progress-name-error" data-error hidden>Enter your name.</p></div>
    <div class="field"><label for="example-error-summary-live-progress-email">Email</label><input type="email" id="example-error-summary-live-progress-email" autocomplete="email" aria-describedby="example-error-summary-live-progress-email-error" required data-email><p class="field-error" id="example-error-summary-live-progress-email-error" data-error hidden>Enter a valid email address.</p></div>
    <div class="field"><label for="example-error-summary-live-progress-date">Delivery date</label><input type="date" id="example-error-summary-live-progress-date" aria-describedby="example-error-summary-live-progress-date-error" required data-date><p class="field-error" id="example-error-summary-live-progress-date-error" data-error hidden>Choose a delivery date.</p></div>
  </div></fieldset>
  <div class="actions"><button type="submit">Continue</button></div>
</form>
<p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
  const root = document.currentScript.closest(".example-error-summary-live-progress");
  const instance = Array.from(document.getElementsByClassName("example-error-summary-live-progress")).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 fields = Array.from(root.querySelectorAll("input"));
  let active = false;
  function validate(showReport) {
    let invalid = 0;
    for (const input of fields) {
      const bad = !input.checkValidity();
      if (bad) invalid += 1;
      input.setAttribute("aria-invalid", String(bad));
      input.parentElement.querySelector(".field-error").hidden = !bad || !active;
    }
    summary.hidden = !active || invalid === 0;
    if (active && invalid > 0) summary.textContent = invalid + " field" + (invalid === 1 ? " still needs" : "s still need") + " attention.";
    if (showReport && invalid > 0) {
      const first = fields.find((input) => !input.checkValidity());
      first.reportValidity();
      first.focus();
    }
    return invalid === 0;
  }
  form.addEventListener("input", () => { if (active) validate(false); });
  function submitForm() {
    active = true;
    if (validate(true)) {
      summary.hidden = true;
      root.querySelector("[data-status]").textContent = "Details accepted in this example; no data left the page.";
    }
  }
  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();
  });
})();
</script></section>
<style>
.example-error-summary-live-progress { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-error-summary-live-progress a { color: #005c62; }
.example-error-summary-live-progress button, .example-error-summary-live-progress summary, .example-error-summary-live-progress input, .example-error-summary-live-progress select, .example-error-summary-live-progress textarea {
  color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-error-summary-live-progress button:hover, .example-error-summary-live-progress summary:hover, .example-error-summary-live-progress button[aria-selected="true"], .example-error-summary-live-progress button[aria-pressed="true"] {
  color: #ffffff; background-color: #00535a;
}
.example-error-summary-live-progress dialog { color: #11292f; background-color: #ffffff; }
.example-error-summary-live-progress [data-note] { color: #11292f; background-color: #e4edef; }
.example-error-summary-live-progress [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-error-summary-live-progress [data-success] { color: #14532d; background-color: #dcfce7; }
.example-error-summary-live-progress [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-error-summary-live-progress :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }

.example-error-summary-live-progress[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-error-summary-live-progress[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-error-summary-live-progress[data-dcl-scheme="dark"] button, .example-error-summary-live-progress[data-dcl-scheme="dark"] summary,
.example-error-summary-live-progress[data-dcl-scheme="dark"] input, .example-error-summary-live-progress[data-dcl-scheme="dark"] select, .example-error-summary-live-progress[data-dcl-scheme="dark"] textarea {
  color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-error-summary-live-progress[data-dcl-scheme="dark"] button:hover, .example-error-summary-live-progress[data-dcl-scheme="dark"] summary:hover,
.example-error-summary-live-progress[data-dcl-scheme="dark"] button[aria-selected="true"], .example-error-summary-live-progress[data-dcl-scheme="dark"] button[aria-pressed="true"] {
  color: #11292f; background-color: #8fe1da;
}
.example-error-summary-live-progress[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-error-summary-live-progress[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-error-summary-live-progress[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-error-summary-live-progress[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-error-summary-live-progress[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-error-summary-live-progress[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }

.example-error-summary-live-progress, .example-error-summary-live-progress * { box-sizing: border-box; min-width: 0; }
.example-error-summary-live-progress { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-error-summary-live-progress h2, .example-error-summary-live-progress h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-error-summary-live-progress header p, .example-error-summary-live-progress p { margin-block: 0.5rem; }
.example-error-summary-live-progress ul, .example-error-summary-live-progress ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-error-summary-live-progress dl { margin-block: 0.5rem; }
.example-error-summary-live-progress dt { font-weight: 700; }
.example-error-summary-live-progress dd { margin: 0 0 0.6rem; }
.example-error-summary-live-progress fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-error-summary-live-progress legend { padding-inline: 0.25rem; font-weight: 700; }
.example-error-summary-live-progress button, .example-error-summary-live-progress input, .example-error-summary-live-progress select, .example-error-summary-live-progress textarea {
  min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-error-summary-live-progress input, .example-error-summary-live-progress select, .example-error-summary-live-progress textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-error-summary-live-progress input[type="checkbox"], .example-error-summary-live-progress input[type="radio"] {
  display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-error-summary-live-progress input[type="file"] { padding: 0.45rem; }
.example-error-summary-live-progress input[type="range"] { padding-inline: 0; }
.example-error-summary-live-progress textarea { min-block-size: 6rem; resize: vertical; }
.example-error-summary-live-progress button, .example-error-summary-live-progress summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-error-summary-live-progress button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-error-summary-live-progress table { border-collapse: collapse; inline-size: 100%; }
.example-error-summary-live-progress th, .example-error-summary-live-progress td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-error-summary-live-progress [hidden] { display: none !important; }
.example-error-summary-live-progress [data-note], .example-error-summary-live-progress [data-error], .example-error-summary-live-progress [data-success], .example-error-summary-live-progress [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-error-summary-live-progress .parts { display: grid; gap: 1rem; }
.example-error-summary-live-progress .field, .example-error-summary-live-progress .fields { display: grid; gap: 0.4rem; }
.example-error-summary-live-progress .fields { gap: 0.75rem; }
.example-error-summary-live-progress .actions, .example-error-summary-live-progress .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-error-summary-live-progress .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-error-summary-live-progress .plain-list { margin: 0; padding: 0; list-style: none; }
.example-error-summary-live-progress .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-error-summary-live-progress .choice input { margin-block-start: 0.25rem; }
.example-error-summary-live-progress .choice-stack { display: grid; gap: 0.55rem; }
.example-error-summary-live-progress .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-live-progress .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-error-summary-live-progress .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-error-summary-live-progress .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-error-summary-live-progress .hint { font-size: 0.875rem; }
.example-error-summary-live-progress .status { min-block-size: 1.5rem; margin-block: 0; }
.example-error-summary-live-progress .two-column, .example-error-summary-live-progress .split-layout { display: grid; gap: 1rem; }
.example-error-summary-live-progress .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-error-summary-live-progress .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-error-summary-live-progress .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-live-progress .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .example-error-summary-live-progress .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
  .example-error-summary-live-progress { padding: 0.75rem; }
  .example-error-summary-live-progress .actions, .example-error-summary-live-progress .inline-row { align-items: stretch; }
  .example-error-summary-live-progress .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-error-summary-live-progress * { transition: none !important; } }
.example-error-summary-live-progress .summary[hidden] { display: none !important; }
.example-error-summary-live-progress .field-error[hidden] { display: none !important; }

.example-error-summary-live-progress form, .example-error-summary-live-progress .field { display: grid; gap: 0.45rem; }
.example-error-summary-live-progress .field-error { margin: 0; font-size: 0.875rem; }
</style>

When to use

Use this when correction is iterative and the form is short enough that live feedback will not interrupt typing. Start validation only after the first submit attempt rather than penalising incomplete input too early.

Avoid

Do not validate aggressively on the first keystroke before the person has had a chance to complete the field.

Accessibility

Inline messages are linked with aria-describedby, the summary count is text in a live-updated alert, and correcting the final error hides it. Focus is never moved during typing. 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.