Estimate Tabs in HTML, CSS and vanilla JS
A copy-paste Estimate Tabs in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="example-estimate-tabs" data-dcl-layout="split" data-dcl-scheme="light" aria-labelledby="example-estimate-tabs-title"><header><h2 id="example-estimate-tabs-title">Estimate Tabs</h2></header><div class="parts"><div role="tablist" aria-label="Topics" aria-orientation="horizontal"><button type="button" role="tab" id="example-estimate-tabs-tab-0" aria-controls="example-estimate-tabs-panel-0" aria-selected="true" tabindex="0">Design</button><button type="button" role="tab" id="example-estimate-tabs-tab-1" aria-controls="example-estimate-tabs-panel-1" aria-selected="false" tabindex="-1">Build</button><button type="button" role="tab" id="example-estimate-tabs-tab-2" aria-controls="example-estimate-tabs-panel-2" aria-selected="false" tabindex="-1">Review</button></div><section role="tabpanel" id="example-estimate-tabs-panel-0" aria-labelledby="example-estimate-tabs-tab-0"><p>Allow two days for layout review.</p></section><section role="tabpanel" id="example-estimate-tabs-panel-1" aria-labelledby="example-estimate-tabs-tab-1" hidden><p>Allow five days for development.</p></section><section role="tabpanel" id="example-estimate-tabs-panel-2" aria-labelledby="example-estimate-tabs-tab-2" hidden><p>Set aside one day for testing.</p></section></div>
<script>
(() => {
const root = document.currentScript.closest(".example-estimate-tabs");
const instance = Array.from(document.getElementsByClassName("example-estimate-tabs")).indexOf(root);
if (instance > 0) {
const references = ["id", "aria-labelledby", "aria-controls", "aria-describedby", "for", "name"];
for (const element of [root, ...root.querySelectorAll("*")]) {
for (const attr of references) {
if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr) + "--" + instance);
}
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 tabs = Array.from(root.querySelectorAll('[role="tab"]'));
root.querySelectorAll('[role="tabpanel"]').forEach((panel) => { panel.tabIndex = 0; });
const vertical = root.querySelector('[role="tablist"]').getAttribute("aria-orientation") === "vertical";
function activate(selected) {
tabs.forEach((tab) => {
const active = tab === selected;
tab.setAttribute("aria-selected", String(active));
tab.tabIndex = active ? 0 : -1;
document.getElementById(tab.getAttribute("aria-controls")).hidden = !active;
});
selected.focus();
}
tabs.forEach((tab, index) => {
tab.addEventListener("click", () => activate(tab));
tab.addEventListener("keydown", (event) => {
const next = vertical ? "ArrowDown" : "ArrowRight";
const previous = vertical ? "ArrowUp" : "ArrowLeft";
let indexToSelect;
if (event.key === next) indexToSelect = (index + 1) % tabs.length;
else if (event.key === previous) indexToSelect = (index + tabs.length - 1) % tabs.length;
else if (event.key === "Home") indexToSelect = 0;
else if (event.key === "End") indexToSelect = tabs.length - 1;
else return;
event.preventDefault();
activate(tabs[indexToSelect]);
});
});
})();
</script></section>
<style>
.example-estimate-tabs { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-estimate-tabs a { color: #005c62; }
.example-estimate-tabs button, .example-estimate-tabs summary, .example-estimate-tabs input, .example-estimate-tabs select {
color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-estimate-tabs button:hover, .example-estimate-tabs summary:hover, .example-estimate-tabs button[aria-selected="true"] {
color: #ffffff; background-color: #00535a;
}
.example-estimate-tabs dialog { color: #11292f; background-color: #ffffff; }
.example-estimate-tabs [data-note] { color: #11292f; background-color: #e4edef; }
.example-estimate-tabs :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.example-estimate-tabs[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-estimate-tabs[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-estimate-tabs[data-dcl-scheme="dark"] button, .example-estimate-tabs[data-dcl-scheme="dark"] summary, .example-estimate-tabs[data-dcl-scheme="dark"] input, .example-estimate-tabs[data-dcl-scheme="dark"] select {
color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-estimate-tabs[data-dcl-scheme="dark"] button:hover, .example-estimate-tabs[data-dcl-scheme="dark"] summary:hover, .example-estimate-tabs[data-dcl-scheme="dark"] button[aria-selected="true"] {
color: #11292f; background-color: #8fe1da;
}
.example-estimate-tabs[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-estimate-tabs[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-estimate-tabs[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.example-estimate-tabs, .example-estimate-tabs * { box-sizing: border-box; }
.example-estimate-tabs { font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-estimate-tabs h2, .example-estimate-tabs h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-estimate-tabs p { margin-block: 0.5rem; }
.example-estimate-tabs ul { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-estimate-tabs article, .example-estimate-tabs details, .example-estimate-tabs [data-note] { border: 1px solid; border-radius: 0.375rem; padding: 0.75rem; }
.example-estimate-tabs button, .example-estimate-tabs summary, .example-estimate-tabs input, .example-estimate-tabs select {
min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-estimate-tabs button, .example-estimate-tabs summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-estimate-tabs button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-estimate-tabs input { display: block; inline-size: 100%; }
.example-estimate-tabs dialog { max-inline-size: min(32rem, 90vw); border-radius: 0.75rem; padding: 1rem; }
.example-estimate-tabs th, .example-estimate-tabs td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-estimate-tabs table { border-collapse: collapse; inline-size: 100%; }
.example-estimate-tabs caption { font-weight: bold; text-align: start; }
.example-estimate-tabs dt { font-weight: 600; }
.example-estimate-tabs dd { margin: 0 0 0.75rem; }
.example-estimate-tabs [hidden] { display: none !important; }
.example-estimate-tabs .parts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 13rem), 1fr)); gap: 1rem; }
.example-estimate-tabs [role="tablist"] { display: flex; flex-wrap: wrap; gap: 0.5rem; } .example-estimate-tabs [role="tabpanel"] { border-inline-start: 1px solid; padding-inline-start: 1rem; }
@media (max-width: 30rem) {
.example-estimate-tabs[data-dcl-layout="rail"] .parts { grid-template-columns: minmax(0, 1fr); }
.example-estimate-tabs[data-dcl-layout="rail"] .parts > :is(p, ul, article) { grid-column: 1; grid-row: auto; }
.example-estimate-tabs[data-dcl-layout="rail"] .parts > :is(details, nav) { grid-row: auto; }
}
@media (prefers-reduced-motion: reduce) { .example-estimate-tabs * { transition: none !important; } }
</style>
When to use
Use this split arrangement when readers benefit from seeing items side by side.
Avoid
Use links instead if the destinations should have separate URLs.
Accessibility
Use arrow keys to move between tabs and Tab to move into the active panel.
Was this useful? Send feedback.