Checkbox group with selection limit in HTML, CSS and vanilla JS
A copy-paste Checkbox group with selection limit in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="example-checkbox-limit-counter" data-dcl-scheme="light" aria-labelledby="example-checkbox-limit-counter-title">
<header><h2 id="example-checkbox-limit-counter-title">Checkbox group with selection limit</h2>
<p>At most three beta features can be selected; unchecked options disable only when the limit is reached.</p></header>
<div class="parts">
<fieldset>
<legend>Choose up to three beta features</legend>
<div class="choice-stack">
<div class="choice"><input type="checkbox" id="example-checkbox-limit-counter-reports" checked data-limit><label for="example-checkbox-limit-counter-reports">Scheduled reports</label></div>
<div class="choice"><input type="checkbox" id="example-checkbox-limit-counter-rules" checked data-limit><label for="example-checkbox-limit-counter-rules">Automation rules</label></div>
<div class="choice"><input type="checkbox" id="example-checkbox-limit-counter-search" data-limit><label for="example-checkbox-limit-counter-search">Natural language search</label></div>
<div class="choice"><input type="checkbox" id="example-checkbox-limit-counter-api" data-limit><label for="example-checkbox-limit-counter-api">Early API access</label></div>
<div class="choice"><input type="checkbox" id="example-checkbox-limit-counter-mobile" data-limit><label for="example-checkbox-limit-counter-mobile">Mobile preview</label></div>
</div>
</fieldset>
<p class="status" role="status" aria-live="polite" data-count></p>
</div>
<script>
(() => {
const root = document.currentScript.closest(".example-checkbox-limit-counter");
const instance = Array.from(document.getElementsByClassName("example-checkbox-limit-counter")).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 maximum = 3;
const inputs = Array.from(root.querySelectorAll("[data-limit]"));
const count = root.querySelector("[data-count]");
function update() {
const selected = inputs.filter((input) => input.checked).length;
for (const input of inputs) input.disabled = selected >= maximum && !input.checked;
count.textContent = selected + " of " + maximum + " beta features selected" + (selected >= maximum ? "; the limit is reached." : ".");
}
inputs.forEach((input) => input.addEventListener("change", update));
update();
})();
</script></section>
<style>
.example-checkbox-limit-counter { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-checkbox-limit-counter a { color: #005c62; }
.example-checkbox-limit-counter button, .example-checkbox-limit-counter summary, .example-checkbox-limit-counter input, .example-checkbox-limit-counter select, .example-checkbox-limit-counter textarea {
color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-checkbox-limit-counter button:hover, .example-checkbox-limit-counter summary:hover, .example-checkbox-limit-counter button[aria-selected="true"], .example-checkbox-limit-counter button[aria-pressed="true"] {
color: #ffffff; background-color: #00535a;
}
.example-checkbox-limit-counter dialog { color: #11292f; background-color: #ffffff; }
.example-checkbox-limit-counter [data-note] { color: #11292f; background-color: #e4edef; }
.example-checkbox-limit-counter [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-checkbox-limit-counter [data-success] { color: #14532d; background-color: #dcfce7; }
.example-checkbox-limit-counter [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-checkbox-limit-counter :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.example-checkbox-limit-counter[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-checkbox-limit-counter[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-checkbox-limit-counter[data-dcl-scheme="dark"] button, .example-checkbox-limit-counter[data-dcl-scheme="dark"] summary,
.example-checkbox-limit-counter[data-dcl-scheme="dark"] input, .example-checkbox-limit-counter[data-dcl-scheme="dark"] select, .example-checkbox-limit-counter[data-dcl-scheme="dark"] textarea {
color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-checkbox-limit-counter[data-dcl-scheme="dark"] button:hover, .example-checkbox-limit-counter[data-dcl-scheme="dark"] summary:hover,
.example-checkbox-limit-counter[data-dcl-scheme="dark"] button[aria-selected="true"], .example-checkbox-limit-counter[data-dcl-scheme="dark"] button[aria-pressed="true"] {
color: #11292f; background-color: #8fe1da;
}
.example-checkbox-limit-counter[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-checkbox-limit-counter[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-checkbox-limit-counter[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-checkbox-limit-counter[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-checkbox-limit-counter[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-checkbox-limit-counter[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.example-checkbox-limit-counter, .example-checkbox-limit-counter * { box-sizing: border-box; min-width: 0; }
.example-checkbox-limit-counter { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-checkbox-limit-counter h2, .example-checkbox-limit-counter h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-checkbox-limit-counter header p, .example-checkbox-limit-counter p { margin-block: 0.5rem; }
.example-checkbox-limit-counter ul, .example-checkbox-limit-counter ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-checkbox-limit-counter dl { margin-block: 0.5rem; }
.example-checkbox-limit-counter dt { font-weight: 700; }
.example-checkbox-limit-counter dd { margin: 0 0 0.6rem; }
.example-checkbox-limit-counter fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-checkbox-limit-counter legend { padding-inline: 0.25rem; font-weight: 700; }
.example-checkbox-limit-counter button, .example-checkbox-limit-counter input, .example-checkbox-limit-counter select, .example-checkbox-limit-counter textarea {
min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-checkbox-limit-counter input, .example-checkbox-limit-counter select, .example-checkbox-limit-counter textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-checkbox-limit-counter input[type="checkbox"], .example-checkbox-limit-counter input[type="radio"] {
display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-checkbox-limit-counter input[type="file"] { padding: 0.45rem; }
.example-checkbox-limit-counter input[type="range"] { padding-inline: 0; }
.example-checkbox-limit-counter textarea { min-block-size: 6rem; resize: vertical; }
.example-checkbox-limit-counter button, .example-checkbox-limit-counter summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-checkbox-limit-counter button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-checkbox-limit-counter table { border-collapse: collapse; inline-size: 100%; }
.example-checkbox-limit-counter th, .example-checkbox-limit-counter td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-checkbox-limit-counter [hidden] { display: none !important; }
.example-checkbox-limit-counter [data-note], .example-checkbox-limit-counter [data-error], .example-checkbox-limit-counter [data-success], .example-checkbox-limit-counter [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-checkbox-limit-counter .parts { display: grid; gap: 1rem; }
.example-checkbox-limit-counter .field, .example-checkbox-limit-counter .fields { display: grid; gap: 0.4rem; }
.example-checkbox-limit-counter .fields { gap: 0.75rem; }
.example-checkbox-limit-counter .actions, .example-checkbox-limit-counter .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-checkbox-limit-counter .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-checkbox-limit-counter .plain-list { margin: 0; padding: 0; list-style: none; }
.example-checkbox-limit-counter .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-checkbox-limit-counter .choice input { margin-block-start: 0.25rem; }
.example-checkbox-limit-counter .choice-stack { display: grid; gap: 0.55rem; }
.example-checkbox-limit-counter .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-checkbox-limit-counter .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-checkbox-limit-counter .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-checkbox-limit-counter .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-checkbox-limit-counter .hint { font-size: 0.875rem; }
.example-checkbox-limit-counter .status { min-block-size: 1.5rem; margin-block: 0; }
.example-checkbox-limit-counter .two-column, .example-checkbox-limit-counter .split-layout { display: grid; gap: 1rem; }
.example-checkbox-limit-counter .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-checkbox-limit-counter .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-checkbox-limit-counter .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-checkbox-limit-counter .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.example-checkbox-limit-counter .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
.example-checkbox-limit-counter { padding: 0.75rem; }
.example-checkbox-limit-counter .actions, .example-checkbox-limit-counter .inline-row { align-items: stretch; }
.example-checkbox-limit-counter .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-checkbox-limit-counter * { transition: none !important; } }
.example-checkbox-limit-counter legend { max-inline-size: 100%; }
</style>
When to use
Use this when a hard selection limit is a real product rule and can be communicated before interaction. State the maximum in the legend and keep the running count visible.
Avoid
Do not disable options without explaining the limit first; people otherwise read the disabled state as a defect.
Accessibility
The maximum is visible before selection, native checkboxes expose their disabled state, and the counter is a polite live region. Nothing depends on colour alone. Main text contrast is 15.19:1 light and 13.07:1 dark; controls are 12.78:1 and 10.94:1.
Was this useful? Send feedback.