Component Library

← Back to gallery

Select with live descriptions in HTML, CSS and vanilla JS

A copy-paste Select with live descriptions in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Forms · Tags: select field, description, live region, guidance, form

Live preview

Source (copy-ready)

<section class="example-select-field-live-description" data-dcl-scheme="light" aria-labelledby="example-select-field-live-description-title">
<header><h2 id="example-select-field-live-description-title">Select with live descriptions</h2>
<p>Changing the select immediately explains the consequences of the selected plan.</p></header>
<div class="parts">
<div class="field"><label for="example-select-field-live-description-plan">Support plan</label><select id="example-select-field-live-description-plan" aria-describedby="example-select-field-live-description-desc" data-select><option value="community">Community</option><option value="priority">Priority</option><option value="managed">Managed</option></select></div><div class="panel" id="example-select-field-live-description-desc" role="status" aria-live="polite" data-desc></div>
</div>
<script>
(() => {
  const root = document.currentScript.closest(".example-select-field-live-description");
  const instance = Array.from(document.getElementsByClassName("example-select-field-live-description")).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 select=root.querySelector('[data-select]'),desc=root.querySelector('[data-desc]'),text={community:'Community support uses public forums with no response-time commitment.',priority:'Priority support adds a one-business-day first response.',managed:'Managed support includes a named engineer and monthly review.'};function sync(){desc.textContent=text[select.value]}select.addEventListener('change',sync);sync();
})();
</script></section>
<style>
.example-select-field-live-description { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-select-field-live-description a { color: #005c62; }
.example-select-field-live-description button, .example-select-field-live-description summary, .example-select-field-live-description input, .example-select-field-live-description select, .example-select-field-live-description textarea {
  color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-select-field-live-description button:hover, .example-select-field-live-description summary:hover, .example-select-field-live-description button[aria-selected="true"], .example-select-field-live-description button[aria-pressed="true"] {
  color: #ffffff; background-color: #00535a;
}
.example-select-field-live-description dialog { color: #11292f; background-color: #ffffff; }
.example-select-field-live-description [data-note] { color: #11292f; background-color: #e4edef; }
.example-select-field-live-description [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-select-field-live-description [data-success] { color: #14532d; background-color: #dcfce7; }
.example-select-field-live-description [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-select-field-live-description :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }

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

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

When to use

Use when options have materially different behavior that is not obvious from their names. Update explanatory text only on change, not on every focus movement.

Avoid

Do not use the description area for validation errors; it is explanatory content and should remain stable enough to read.

Accessibility

The native select is labelled and aria-describedby ties it to the persistent description. The description is plain text in a polite live region and remains visible in both colour schemes.

Was this useful? Send feedback.