Pure CSS Tabs (No JavaScript)
You can build working tabs with pure CSS, no JavaScript: a group of hidden radio buttons remembers which tab is selected, the labels are the tab buttons, and the :checked selector shows the matching panel. Click the tabs in the demo below.
Demo:
How it works
- Radio buttons hold the state. Only one radio in a group can be checked, which is exactly how tabs behave. They’re hidden visually but stay in the page, so the arrow keys still switch tabs.
- Labels are the tabs. Clicking a
<label for="...">checks its radio. order: 1on the panels pushes them below all the tab labels in the flex row, even though they come after the inputs in the HTML.:checked ~ .paneluses the general sibling selector to show the panel that matches the checked radio.
Tabs with details and summary
Another no-JavaScript option: several <details> elements with the same name attribute behave like an exclusive accordion, where opening one closes the others. Styled as a row, it works like tabs that collapse nicely on mobile.
<details name="faq" open>
<summary>Shipping</summary>
<p>We ship worldwide in 3–5 days.</p>
</details>
<details name="faq">
<summary>Returns</summary>
<p>Free returns within 30 days.</p>
</details>
The accessible version (recommended for production)
The CSS-only version works, but screen readers don’t announce it as tabs. Real tabs use role="tablist", role="tab" and role="tabpanel" with aria-selected, which needs a few lines of JavaScript.
<div class="tabs">
<div role="tablist" aria-label="Languages">
<button role="tab" aria-selected="true" aria-controls="p1" id="t1">HTML</button>
<button role="tab" aria-selected="false" aria-controls="p2" id="t2" tabindex="-1">CSS</button>
</div>
<div role="tabpanel" id="p1" aria-labelledby="t1">HTML gives the page structure.</div>
<div role="tabpanel" id="p2" aria-labelledby="t2" hidden>CSS controls the look.</div>
</div>
<script>
document.querySelectorAll('[role="tab"]').forEach((tab) => {
tab.addEventListener('click', () => {
const list = tab.closest('[role="tablist"]');
list.querySelectorAll('[role="tab"]').forEach((t) => {
const selected = t === tab;
t.setAttribute('aria-selected', selected);
t.tabIndex = selected ? 0 : -1;
document.getElementById(t.getAttribute('aria-controls')).hidden = !selected;
});
});
});
</script>
FAQ
Can I make tabs without JavaScript?
Yes, with hidden radio buttons, labels and the :checked selector, as in the demo. For full screen reader support, the ARIA version with a little JavaScript is better.
How do I make one tab open by default?
Add the checked attribute to its radio button.
Can I link to a specific tab?
Not with the radio version. Use :target with anchor links instead, or the JavaScript version reading location.hash.
Related: CSS Flexbox, CSS !important, HTML tips and CSS ::before and ::after.