Quantity stepper with unit price total in HTML, CSS and vanilla JS
A copy-paste Quantity stepper with unit price total in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="example-number-stepper-total" data-dcl-scheme="light" aria-labelledby="example-number-stepper-total-title">
<header><h2 id="example-number-stepper-total-title">Quantity stepper with unit price total</h2>
<p>Changing quantity immediately recalculates the total against a visible unit price.</p></header>
<div class="parts">
<div class="price-stepper split-layout">
<div class="field">
<label for="example-number-stepper-total-input">Licences at £12.50 each</label>
<div class="stepper-row">
<button type="button" aria-label="Decrease licences" data-decrement>−</button>
<input type="number" id="example-number-stepper-total-input" min="1" max="10" step="1" value="1" inputmode="numeric" data-input>
<button type="button" aria-label="Increase licences" data-increment>+</button>
</div>
</div>
<div class="panel total-panel" data-note>
<p>Estimated total</p>
<output data-total aria-live="polite">£12.50</output>
</div>
</div>
<p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
const root = document.currentScript.closest(".example-number-stepper-total");
const instance = Array.from(document.getElementsByClassName("example-number-stepper-total")).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]");
const increment = root.querySelector("[data-increment]");
const decrement = root.querySelector("[data-decrement]");
const total = root.querySelector("[data-total]");
const status = root.querySelector("[data-status]");
const currency = new Intl.NumberFormat("en-GB", { style: "currency", currency: "GBP" });
function value() { return Number(input.value) || 1; }
function sync() {
const current = Math.min(10, Math.max(1, value()));
input.value = String(current);
decrement.disabled = current <= 1;
increment.disabled = current >= 10;
const amount = currency.format(current * 12.5);
total.textContent = amount;
status.textContent = current + " licence" + (current === 1 ? "" : "s") + " at £12.50 each costs " + amount + ".";
}
increment.addEventListener("click", () => { input.value = String(value() + 1); sync(); });
decrement.addEventListener("click", () => { input.value = String(value() - 1); sync(); });
input.addEventListener("input", sync);
sync();
})();
</script></section>
<style>
.example-number-stepper-total { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-number-stepper-total a { color: #005c62; }
.example-number-stepper-total button, .example-number-stepper-total summary, .example-number-stepper-total input, .example-number-stepper-total select, .example-number-stepper-total textarea {
color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-number-stepper-total button:hover, .example-number-stepper-total summary:hover, .example-number-stepper-total button[aria-selected="true"], .example-number-stepper-total button[aria-pressed="true"] {
color: #ffffff; background-color: #00535a;
}
.example-number-stepper-total dialog { color: #11292f; background-color: #ffffff; }
.example-number-stepper-total [data-note] { color: #11292f; background-color: #e4edef; }
.example-number-stepper-total [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-number-stepper-total [data-success] { color: #14532d; background-color: #dcfce7; }
.example-number-stepper-total [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-number-stepper-total :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.example-number-stepper-total[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-number-stepper-total[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-number-stepper-total[data-dcl-scheme="dark"] button, .example-number-stepper-total[data-dcl-scheme="dark"] summary,
.example-number-stepper-total[data-dcl-scheme="dark"] input, .example-number-stepper-total[data-dcl-scheme="dark"] select, .example-number-stepper-total[data-dcl-scheme="dark"] textarea {
color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-number-stepper-total[data-dcl-scheme="dark"] button:hover, .example-number-stepper-total[data-dcl-scheme="dark"] summary:hover,
.example-number-stepper-total[data-dcl-scheme="dark"] button[aria-selected="true"], .example-number-stepper-total[data-dcl-scheme="dark"] button[aria-pressed="true"] {
color: #11292f; background-color: #8fe1da;
}
.example-number-stepper-total[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-number-stepper-total[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-number-stepper-total[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-number-stepper-total[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-number-stepper-total[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-number-stepper-total[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.example-number-stepper-total, .example-number-stepper-total * { box-sizing: border-box; min-width: 0; }
.example-number-stepper-total { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-number-stepper-total h2, .example-number-stepper-total h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-number-stepper-total header p, .example-number-stepper-total p { margin-block: 0.5rem; }
.example-number-stepper-total ul, .example-number-stepper-total ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-number-stepper-total dl { margin-block: 0.5rem; }
.example-number-stepper-total dt { font-weight: 700; }
.example-number-stepper-total dd { margin: 0 0 0.6rem; }
.example-number-stepper-total fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-number-stepper-total legend { padding-inline: 0.25rem; font-weight: 700; }
.example-number-stepper-total button, .example-number-stepper-total input, .example-number-stepper-total select, .example-number-stepper-total textarea {
min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-number-stepper-total input, .example-number-stepper-total select, .example-number-stepper-total textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-number-stepper-total input[type="checkbox"], .example-number-stepper-total input[type="radio"] {
display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-number-stepper-total input[type="file"] { padding: 0.45rem; }
.example-number-stepper-total input[type="range"] { padding-inline: 0; }
.example-number-stepper-total textarea { min-block-size: 6rem; resize: vertical; }
.example-number-stepper-total button, .example-number-stepper-total summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-number-stepper-total button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-number-stepper-total table { border-collapse: collapse; inline-size: 100%; }
.example-number-stepper-total th, .example-number-stepper-total td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-number-stepper-total [hidden] { display: none !important; }
.example-number-stepper-total [data-note], .example-number-stepper-total [data-error], .example-number-stepper-total [data-success], .example-number-stepper-total [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-number-stepper-total .parts { display: grid; gap: 1rem; }
.example-number-stepper-total .field, .example-number-stepper-total .fields { display: grid; gap: 0.4rem; }
.example-number-stepper-total .fields { gap: 0.75rem; }
.example-number-stepper-total .actions, .example-number-stepper-total .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-number-stepper-total .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-number-stepper-total .plain-list { margin: 0; padding: 0; list-style: none; }
.example-number-stepper-total .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-number-stepper-total .choice input { margin-block-start: 0.25rem; }
.example-number-stepper-total .choice-stack { display: grid; gap: 0.55rem; }
.example-number-stepper-total .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-number-stepper-total .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-number-stepper-total .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-number-stepper-total .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-number-stepper-total .hint { font-size: 0.875rem; }
.example-number-stepper-total .status { min-block-size: 1.5rem; margin-block: 0; }
.example-number-stepper-total .two-column, .example-number-stepper-total .split-layout { display: grid; gap: 1rem; }
.example-number-stepper-total .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-number-stepper-total .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-number-stepper-total .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-number-stepper-total .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.example-number-stepper-total .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
.example-number-stepper-total { padding: 0.75rem; }
.example-number-stepper-total .actions, .example-number-stepper-total .inline-row { align-items: stretch; }
.example-number-stepper-total .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-number-stepper-total * { transition: none !important; } }
.example-number-stepper-total .price-stepper { align-items: start; }
.example-number-stepper-total .stepper-row { display: grid; grid-template-columns: auto minmax(4rem, 1fr) auto; gap: 0.4rem; }
.example-number-stepper-total .stepper-row input { text-align: center; }
.example-number-stepper-total .total-panel output { display: block; font-size: 1.5rem; font-weight: 700; }
</style>
When to use
Use this when people need price feedback before adding an item to an order. Label the calculation as an example or estimate whenever taxes and discounts are not yet included.
Avoid
Do not show a total that cannot be reconciled with the visible unit price and quantity; hidden rounding rules erode trust.
Accessibility
Quantity is a native labelled number input, the calculation is text in a live output element, and buttons remain keyboard operable native controls. 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.