Accessible HTML Tabs For Web Pages
I'm adding tabs to show different examples for a color picker in Neopoligenneo .
I found a video on how to make "CSS only" tabs that looked good but never mentioned accessibility. I checked with folks on a few discords for guidance and learned it's got problems. Specifically, it's not currently possible to make CSS only tabs in an accessible way.
Chris Ferdinandi from Go Make Thingsgmt and Mayank from mayank.comayank sent over some better alternatives. Here's Mayank's approach with some specific styles added into the CSS below.
This is my local copy of the code for my notes. I've added nothing new here except a few styles. Be sure to visit Mayank's post for the original code and details
CSS
:root {
--color-selected: rgb(255 255 255);
--color-not-selected: rgb(255 255 255 / .6);
}
[role="tab"] {
background: none;
border: none;
color: var(--color-not-selected);
cursor: pointer;
font: inherit;
outline: inherit;
padding-block: 0 2px;
padding-inline: 11px;
&[aria-selected='true'] {
border-bottom: 3px solid var(--color-selected);
color: var(--color-selected);
padding-block: 0 0;
}
}
[role="tablist"] {
border-bottom: 1px solid var(--color-selected);
}
[role="tabpanel"] {
padding: 0.7rem;
}
Endnotes
-
I'm not linking to the original video since the technique isn't accessible and I don't want to add to its SEO. It's called "How to Easily Create Pure CSS Tabs (No JavaScript Needed!)" by "dcode" if you want to look it up
Footnotes
- Neopoligen
The website building app I'm currently working on. As I'm writing this the first preview version is out with another more usable version on the way soon
References
-
This is the original post with the code from above.
-
This is Chris Ferdinandi's approach to tabs. I don't have enough knowledge to speak about the difference but it looks good too