Component Library

← Back to gallery

Enforced word-limit textarea in HTML, CSS and vanilla JS

A copy-paste Enforced word-limit textarea in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Forms · Tags: textarea, word count, limit, validation, form

Live preview

Source (copy-ready)

<section class="example-textarea-counter-word-limit" data-dcl-scheme="light" aria-labelledby="example-textarea-counter-word-limit-title">
<header><h2 id="example-textarea-counter-word-limit-title">Enforced word-limit textarea</h2>
<p>A hard word limit truncates excess input while explaining what happened.</p></header>
<div class="parts">
<div class="field"><label for="example-textarea-counter-word-limit-summary">Executive summary</label><textarea id="example-textarea-counter-word-limit-summary" aria-describedby="example-textarea-counter-word-limit-count" data-input></textarea></div><p id="example-textarea-counter-word-limit-count" data-count>0 of 20 words.</p><p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
  const root = document.currentScript.closest(".example-textarea-counter-word-limit");
  const instance = Array.from(document.getElementsByClassName("example-textarea-counter-word-limit")).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 input=root.querySelector('[data-input]'),count=root.querySelector('[data-count]'),status=root.querySelector('[data-status]');function words(v){return v.trim()?v.trim().split(/\s+/):[]}input.addEventListener('input',()=>{let w=words(input.value),cut=false;if(w.length>20){input.value=w.slice(0,20).join(' ');w=words(input.value);cut=true}count.textContent=w.length+' of 20 words.';input.setAttribute('aria-invalid',String(cut));status.textContent=cut?'Word limit reached; extra words removed.':(20-w.length)+' words remain.'});status.textContent='20 words remain.';
})();
</script></section>
<style>
.example-textarea-counter-word-limit { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-textarea-counter-word-limit a { color: #005c62; }
.example-textarea-counter-word-limit button, .example-textarea-counter-word-limit summary, .example-textarea-counter-word-limit input, .example-textarea-counter-word-limit select, .example-textarea-counter-word-limit textarea {
  color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-textarea-counter-word-limit button:hover, .example-textarea-counter-word-limit summary:hover, .example-textarea-counter-word-limit button[aria-selected="true"], .example-textarea-counter-word-limit button[aria-pressed="true"] {
  color: #ffffff; background-color: #00535a;
}
.example-textarea-counter-word-limit dialog { color: #11292f; background-color: #ffffff; }
.example-textarea-counter-word-limit [data-note] { color: #11292f; background-color: #e4edef; }
.example-textarea-counter-word-limit [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-textarea-counter-word-limit [data-success] { color: #14532d; background-color: #dcfce7; }
.example-textarea-counter-word-limit [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-textarea-counter-word-limit :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }

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

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

When to use

Use when downstream storage or display truly has a word constraint. Count visible words rather than raw characters and warn before people paste a long answer blindly.

Avoid

Do not discard excess text silently; the status must say that extra words were removed.

Accessibility

The labelled textarea counts whitespace-separated words in JavaScript. Input beyond twenty words is prevented deterministically, aria-invalid communicates overflow attempts, and the enforcement message is announced.

Was this useful? Send feedback.