Counter appearing near character limit in HTML, CSS and vanilla JS
A copy-paste Counter appearing near character limit in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="example-textarea-counter-threshold" data-dcl-scheme="light" aria-labelledby="example-textarea-counter-threshold-title">
<header><h2 id="example-textarea-counter-threshold-title">Counter appearing near character limit</h2>
<p>The exact counter stays out of the way until remaining capacity becomes important.</p></header>
<div class="parts">
<div class="field"><label for="example-textarea-counter-threshold-reply">Reply</label><textarea id="example-textarea-counter-threshold-reply" aria-describedby="example-textarea-counter-threshold-hint" data-input></textarea><p class="hint" id="example-textarea-counter-threshold-hint">Up to 40 characters; the counter appears when 10 remain.</p></div><p class="near-counter" data-counter hidden>40 characters remain.</p><p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
const root = document.currentScript.closest(".example-textarea-counter-threshold");
const instance = Array.from(document.getElementsByClassName("example-textarea-counter-threshold")).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]'),counter=root.querySelector('[data-counter]'),status=root.querySelector('[data-status]');let near=false;function sync(){const left=40-input.value.length,now=left<=10;if(now!==near)status.textContent=now?'Character counter visible; '+left+' remain.':'Counter hidden; ample characters remain.';near=now;counter.hidden=!now;counter.textContent=left+' characters remain.'}input.addEventListener('input',sync);sync();
})();
</script></section>
<style>
.example-textarea-counter-threshold { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-textarea-counter-threshold a { color: #005c62; }
.example-textarea-counter-threshold button, .example-textarea-counter-threshold summary, .example-textarea-counter-threshold input, .example-textarea-counter-threshold select, .example-textarea-counter-threshold textarea {
color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-textarea-counter-threshold button:hover, .example-textarea-counter-threshold summary:hover, .example-textarea-counter-threshold button[aria-selected="true"], .example-textarea-counter-threshold button[aria-pressed="true"] {
color: #ffffff; background-color: #00535a;
}
.example-textarea-counter-threshold dialog { color: #11292f; background-color: #ffffff; }
.example-textarea-counter-threshold [data-note] { color: #11292f; background-color: #e4edef; }
.example-textarea-counter-threshold [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-textarea-counter-threshold [data-success] { color: #14532d; background-color: #dcfce7; }
.example-textarea-counter-threshold [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-textarea-counter-threshold :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.example-textarea-counter-threshold[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-textarea-counter-threshold[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-textarea-counter-threshold[data-dcl-scheme="dark"] button, .example-textarea-counter-threshold[data-dcl-scheme="dark"] summary,
.example-textarea-counter-threshold[data-dcl-scheme="dark"] input, .example-textarea-counter-threshold[data-dcl-scheme="dark"] select, .example-textarea-counter-threshold[data-dcl-scheme="dark"] textarea {
color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-textarea-counter-threshold[data-dcl-scheme="dark"] button:hover, .example-textarea-counter-threshold[data-dcl-scheme="dark"] summary:hover,
.example-textarea-counter-threshold[data-dcl-scheme="dark"] button[aria-selected="true"], .example-textarea-counter-threshold[data-dcl-scheme="dark"] button[aria-pressed="true"] {
color: #11292f; background-color: #8fe1da;
}
.example-textarea-counter-threshold[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-textarea-counter-threshold[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-textarea-counter-threshold[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-textarea-counter-threshold[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-textarea-counter-threshold[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-textarea-counter-threshold[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.example-textarea-counter-threshold, .example-textarea-counter-threshold * { box-sizing: border-box; min-width: 0; }
.example-textarea-counter-threshold { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-textarea-counter-threshold h2, .example-textarea-counter-threshold h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-textarea-counter-threshold header p, .example-textarea-counter-threshold p { margin-block: 0.5rem; }
.example-textarea-counter-threshold ul, .example-textarea-counter-threshold ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-textarea-counter-threshold dl { margin-block: 0.5rem; }
.example-textarea-counter-threshold dt { font-weight: 700; }
.example-textarea-counter-threshold dd { margin: 0 0 0.6rem; }
.example-textarea-counter-threshold fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-textarea-counter-threshold legend { padding-inline: 0.25rem; font-weight: 700; }
.example-textarea-counter-threshold button, .example-textarea-counter-threshold input, .example-textarea-counter-threshold select, .example-textarea-counter-threshold textarea {
min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-textarea-counter-threshold input, .example-textarea-counter-threshold select, .example-textarea-counter-threshold textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-textarea-counter-threshold input[type="checkbox"], .example-textarea-counter-threshold input[type="radio"] {
display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-textarea-counter-threshold input[type="file"] { padding: 0.45rem; }
.example-textarea-counter-threshold input[type="range"] { padding-inline: 0; }
.example-textarea-counter-threshold textarea { min-block-size: 6rem; resize: vertical; }
.example-textarea-counter-threshold button, .example-textarea-counter-threshold summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-textarea-counter-threshold button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-textarea-counter-threshold table { border-collapse: collapse; inline-size: 100%; }
.example-textarea-counter-threshold th, .example-textarea-counter-threshold td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-textarea-counter-threshold [hidden] { display: none !important; }
.example-textarea-counter-threshold [data-note], .example-textarea-counter-threshold [data-error], .example-textarea-counter-threshold [data-success], .example-textarea-counter-threshold [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-textarea-counter-threshold .parts { display: grid; gap: 1rem; }
.example-textarea-counter-threshold .field, .example-textarea-counter-threshold .fields { display: grid; gap: 0.4rem; }
.example-textarea-counter-threshold .fields { gap: 0.75rem; }
.example-textarea-counter-threshold .actions, .example-textarea-counter-threshold .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-textarea-counter-threshold .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-textarea-counter-threshold .plain-list { margin: 0; padding: 0; list-style: none; }
.example-textarea-counter-threshold .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-textarea-counter-threshold .choice input { margin-block-start: 0.25rem; }
.example-textarea-counter-threshold .choice-stack { display: grid; gap: 0.55rem; }
.example-textarea-counter-threshold .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-threshold .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-textarea-counter-threshold .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-textarea-counter-threshold .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-textarea-counter-threshold .hint { font-size: 0.875rem; }
.example-textarea-counter-threshold .status { min-block-size: 1.5rem; margin-block: 0; }
.example-textarea-counter-threshold .two-column, .example-textarea-counter-threshold .split-layout { display: grid; gap: 1rem; }
.example-textarea-counter-threshold .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-textarea-counter-threshold .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-textarea-counter-threshold .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-threshold .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.example-textarea-counter-threshold .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
.example-textarea-counter-threshold { padding: 0.75rem; }
.example-textarea-counter-threshold .actions, .example-textarea-counter-threshold .inline-row { align-items: stretch; }
.example-textarea-counter-threshold .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-textarea-counter-threshold * { transition: none !important; } }
.example-textarea-counter-threshold .near-counter[hidden] { display: none !important; }
.example-textarea-counter-threshold .near-counter{font-weight:700}
</style>
When to use
Use for optional long fields where persistent counting would distract most users. Reveal it early enough to prevent surprise, typically with ten or more characters remaining.
Avoid
Do not hide the counter so long that people first learn about the limit after exceeding it.
Accessibility
The labelled textarea references a persistent hint. The counter uses hidden rather than CSS removal, appears at the threshold, and crossing into or out of that state is announced politely.
Was this useful? Send feedback.