Accessible custom radio buttons and checkboxes

Russ Weakley
Russ WeakleyUX/UI developer at Max Design
Accessible
custom radio buttons
and checkboxes
The pain
Web designers and developers
have always struggled with how
to customise radio buttons and
checkboxes.
Diagram: A series of differently styled on/off switches.
The main issue is that these
elements are notoriously hard
to style - especially across
multiple browsers and devices.
In the past, some developers
resorted to JavaScript-based
solutions to solve this problem.
In some cases this involved
using JS to remove the
original radio or checkbox
element making the end result
inaccessible for a wide range of
assistive technologies.
It can be done
However, it is possible to style
these elements without having
to use JavaScript. And more
importantly, we can make the
end result accessible.
Let’s take a simple example of
an on/off switch that can be
applied to either radio or
checkbox elements:
unchecked
checked
Diagram: Shows the unchecked and checked version of the switch.
The solution I’m about to demo,
has four key accessibility
features:
Feature 1:
Using the intended semantic
elements, the input and label
elements will be explicitly
associated using matching "for"
and "id" values.
Feature 2:
The label content can used to
describe the purpose of each
switch for screen readers. This
content is hidden off-screen.
Feature 3:
The tick icon makes the two
different states clearly
distinguishable - aiding colour-
blind users and some types of
cognitive-impaired users.
Feature 4:
The switch is keyboard-friendly
(operates in the normal way) but
we also clearly flag the focus and
hover states.
HTML/CSS
<div class="switch">
<input
class="switch__control"
type="radio"
name="example01"
id="example01">
<label class="switch__label" for="example01">
<span class="switch__content">Label content</span>
</label>
</div>
Radio
<div class="switch">
<input
class="switch__control"
type="checkbox"
name="example01"
id="example01">
<label class="switch__label" for="example01">
<span class="switch__content">Label content</span>
</label>
</div>
Checkbox
<div class="switch">
<input
class="switch__control"
type="checkbox"
name="example01"
id="example01">
<label class="switch__label" for="example01">
<span class="switch__content">Label content</span>
</label>
</div>
ID value
“for” values
/* parent module */
.switch { }
/* descendants of parent module */
.switch__control { }
.switch__label { }
.switch__content { }
How does it work?
We can use the parent
container to create the overall
dimensions of the switch.
switch
Diagram: Shows the switch layer as a dotted outline.
The control is then positioned
on top of the parent, and set
with opacity: 0. This means it
exists on the page, and can be
interacted with, but it cannot be
seen.
switch
switch__control
Diagram: Shows the control sitting on top of the switch layer.
Then, the label is placed on top
of the radio button, and we can
style the background of this label
to look like a switch.
switch
switch__control
switch__label
Diagram: Shows label sitting on top of the control.
And finally, the label content is
hidden off screen so that it is
available for screen readers, but
does not clutter the visual
appearance of the switch.
The problem
Checkbox and radio button
elements can be manually
changed by users - from
unchecked to checked etc.
These elements can also be
given boolean “checked” and
“disabled” attributes.
<!-- unchecked -->
<input type="checkbox">
<!-- checked -->
<input type="checkbox" checked>
<!-- disabled -->
<input type="checkbox" disabled>
However, for this solution, most
of the styling is applied to the
label element, rather than the
input.
Unfortunately, the label element
has no checked, unchecked
or disabled state of its own.
We can get around this using
adjacent sibling selectors,
which target any label element
that is adjacent to (comes
directly after) the input.
/* unchecked input */
.switch__control + label { }
/* checked input */
.switch__control:checked + label { }
/* diabled input */
.switch__control[disabled] + label { }
unchecked
checked
disabled
Diagram: Shows the unchecked, checked and disabled version of the switch.
Different states
Finally, we also want to style
the :focus and :hover states of
the switch.
/* unchecked input */
.switch__control:hover + label { }
.switch__control:focus + label { }
/* checked input */
.switch__control:checked:hover + label { }
.switch__control:checked:focus + label { }
This gives us a series of
possible results that looks like
this:
unchecked hover
unchecked focus
unchecked
checked
checked hover
checked focus
disabled
Diagram: Shows variations of the switch.
Demo:
https://russmaxdesign.github.io/switch-checkbox/
Russ Weakley
Max Design

Site: maxdesign.com.au

Twitter: twitter.com/russmaxdesign

Slideshare: slideshare.net/maxdesign

Linkedin: linkedin.com/in/russweakley
1 of 45

More Related Content

What's hot(20)

Accessible Form Hints and ErrorsAccessible Form Hints and Errors
Accessible Form Hints and Errors
Russ Weakley1.8K views
Accessible Inline errors messagesAccessible Inline errors messages
Accessible Inline errors messages
Russ Weakley1.7K views
Visual Basic menuVisual Basic menu
Visual Basic menu
kuldeep947.5K views
Microsoft visual basic 6Microsoft visual basic 6
Microsoft visual basic 6
Penang, Malaysia8.1K views
Grasping The LightSwitch ParadigmGrasping The LightSwitch Paradigm
Grasping The LightSwitch Paradigm
Andrew Brust1.3K views
WPF - An introductionWPF - An introduction
WPF - An introduction
Sharada Gururaj3.5K views
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
sanket199611.2K views
WPF - Controls &amp; DataWPF - Controls &amp; Data
WPF - Controls &amp; Data
Sharada Gururaj662 views
Creating a quiz using visual basic 6Creating a quiz using visual basic 6
Creating a quiz using visual basic 6
Ella Marie Wico32.6K views
Visusual basicVisusual basic
Visusual basic
Mandavi Classes465 views
Toolbar, statusbar, coolbar in vbToolbar, statusbar, coolbar in vb
Toolbar, statusbar, coolbar in vb
Amandeep Kaur11.1K views
Buttons In .net Visual BasicButtons In .net Visual Basic
Buttons In .net Visual Basic
manish maurya838 views
Visual Basic Controls pptVisual Basic Controls ppt
Visual Basic Controls ppt
Ranjuma Shubhangi18.8K views
Controls Use in Windows Presentation Foundation (WPF)Controls Use in Windows Presentation Foundation (WPF)
Controls Use in Windows Presentation Foundation (WPF)
iFour Technolab Pvt. Ltd.199 views

Similar to Accessible custom radio buttons and checkboxes(20)

Visual basicVisual basic
Visual basic
KavithaAlagumalai200 views
Visual Basic.pptxVisual Basic.pptx
Visual Basic.pptx
KavithaAlagumalai19 views
Lab view introductionLab view introduction
Lab view introduction
JakupBerisha286 views
Easyeda tutorialEasyeda tutorial
Easyeda tutorial
SANTIAGO PABLO ALBERTO763 views
2  front panel2  front panel
2 front panel
Ankush Jamthikar184 views
From Design to UML to Working WireframeFrom Design to UML to Working Wireframe
From Design to UML to Working Wireframe
UX Antwerp Meetup747 views
A web source Project.pdfA web source Project.pdf
A web source Project.pdf
GeorgianaAlinaPatric13 views
04 conexão logo! 0 ba7 com ihm04 conexão logo! 0 ba7 com ihm
04 conexão logo! 0 ba7 com ihm
Marcio Miranda168 views
04 conexão logo! 0 ba7 com ihm04 conexão logo! 0 ba7 com ihm
04 conexão logo! 0 ba7 com ihm
confidencial 545 views
222066369 clad-study-guide222066369 clad-study-guide
222066369 clad-study-guide
homeworkping947 views
Rocket Editor (Recovered).pptxRocket Editor (Recovered).pptx
Rocket Editor (Recovered).pptx
SkyknightBeoulve110 views
Basic of Java NetbeansBasic of Java Netbeans
Basic of Java Netbeans
Shrey Goswami5.8K views
VbtutorialVbtutorial
Vbtutorial
dhi her645 views
Orcad layoutOrcad layout
Orcad layout
Pantech ProLabs India Pvt Ltd2.1K views
Orcad Capture - Schematic Design TutorialOrcad Capture - Schematic Design Tutorial
Orcad Capture - Schematic Design Tutorial
Pantech ProLabs India Pvt Ltd21.7K views

More from Russ Weakley(16)

Accessible chat windowsAccessible chat windows
Accessible chat windows
Russ Weakley200 views
Accessible names & descriptionsAccessible names & descriptions
Accessible names & descriptions
Russ Weakley16 views
A deep dive into accessible namesA deep dive into accessible names
A deep dive into accessible names
Russ Weakley23 views
Accessible states in Design SystemsAccessible states in Design Systems
Accessible states in Design Systems
Russ Weakley1.3K views
What is accessibility?What is accessibility?
What is accessibility?
Russ Weakley5.3K views
Accessibility in Pattern LibrariesAccessibility in Pattern Libraries
Accessibility in Pattern Libraries
Russ Weakley1.6K views
Accessibility in pattern librariesAccessibility in pattern libraries
Accessibility in pattern libraries
Russ Weakley1.6K views
Building an accessible auto-completeBuilding an accessible auto-complete
Building an accessible auto-complete
Russ Weakley9.9K views
Deep Dive into Line-HeightDeep Dive into Line-Height
Deep Dive into Line-Height
Russ Weakley3.8K views
Specialise or cross-skillSpecialise or cross-skill
Specialise or cross-skill
Russ Weakley6.8K views
CSS pattern librariesCSS pattern libraries
CSS pattern libraries
Russ Weakley83.8K views

Recently uploaded(20)

Class 10 English  lesson plansClass 10 English  lesson plans
Class 10 English lesson plans
Tariq KHAN172 views
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxGopal Chakraborty Memorial Quiz 2.0 Prelims.pptx
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptx
Debapriya Chakraborty221 views
ICS3211_lecture 08_2023.pdfICS3211_lecture 08_2023.pdf
ICS3211_lecture 08_2023.pdf
Vanessa Camilleri68 views
Industry4wrd.pptxIndustry4wrd.pptx
Industry4wrd.pptx
BC Chew153 views
Chemistry of sex hormones.pptxChemistry of sex hormones.pptx
Chemistry of sex hormones.pptx
RAJ K. MAURYA97 views
Narration  ppt.pptxNarration  ppt.pptx
Narration ppt.pptx
Tariq KHAN62 views
Sociology KS5Sociology KS5
Sociology KS5
WestHatch50 views
Drama KS5 BreakdownDrama KS5 Breakdown
Drama KS5 Breakdown
WestHatch50 views
GSoC 2024GSoC 2024
GSoC 2024
DeveloperStudentClub1049 views
STYP infopack.pdfSTYP infopack.pdf
STYP infopack.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego143 views
Azure DevOps Pipeline setup for Mule APIs #36Azure DevOps Pipeline setup for Mule APIs #36
Azure DevOps Pipeline setup for Mule APIs #36
MysoreMuleSoftMeetup75 views
Classification of crude drugs.pptxClassification of crude drugs.pptx
Classification of crude drugs.pptx
GayatriPatra1449 views

Accessible custom radio buttons and checkboxes