Have you ever tried to style checkboxes or radio buttons and ended up pulling your hair out? This presentation will explore a few simple tricks that can be used to style checkboxes and radio buttons. In this case, we will make them look like an on/off switch.
12. Unplug the mouse
The easiest and most effective way
to check your site is using
keyboard-only.
13. A large number of users rely on
key-strokes (TAB, ARROW, ENTER,
SPACE) or the equivalent of these
keystrokes in order to navigate and
interact with sites/apps.
14. If you cannot navigate or interact
with your site/app using keystrokes
only, then your site is potentially
inaccessible to a large number of
users.
15. Question 2:
Why is this one of the most evil CSS
rules you could ever write?
*:focus { outline: none; }
17. Because this rule make it hard, if not
impossible, for keyboard-only users
to see which element is in focus
and therefore very hard to navigate
and interact with your site/app.
18. Time to explore how to style a
simple radio button or checkbox!
20. Web designers and developers have
always struggled with how to
customise radio buttons and
checkboxes.
21.
22. The main issue is that radio buttons
and checkboxes are notoriously
hard to style - especially across
multiple browsers and devices.
23. In the past, some developers
resorted to JavaScript-based
solutions to solve this problem.
24. In some cases this involved using
JavaScript to remove the original
radio or checkbox element making
the end result inaccessible for a
wide range of assistive
technologies.
29. The solution I’m about to demo, has
five key accessibility features.
30. Well… many of these are not really
features, they are just default
behaviours that should not be
overridden.
31. Feature 1:
We will use the appropriate
semantic elements - input and label
elements. We will explicitly
associate these elements using
matching “for" and "id" values.
32. Feature 2:
The label content can be used to
describe the purpose of each switch
for screen readers. This content is
hidden off-screen.
33. Feature 3:
We will make the two different
states (“on” and “off”) clearly
distinguishable using a tick icon for
the “on” state. This will aid colour-
blind users and some types of
cognitive-impaired users.
34. (Keeping in mind that we should
never use “color alone” to signal
important information.)
36. Feature 4:
Because we are using native
elements, the default keyboard
behaviour will still be available.
(Users can select a radio button or
checkbox using the SPACE bar).
37. Feature 5:
We will make the focus and hover
states clearly visible. The focus
state is especially important for
keyboard only users.
51. The radio button or checkbox
control (“switch__control”) is then
positioned on top of the parent. It
will be given the same dimensions
as the parent.
53. The label (“switch__label”) is placed
on top of the radio button and also
given the same dimensions as the
parent. We are hiding the control
under the label.
54. We will then style the background
of the label to look like a switch -
including adding rounded corners
and our background icon.
56. And finally, the label content
(“switch__content”) is hidden off
screen so that it is available for
screen readers, but does not clutter
the visual appearance of the switch.
83. I’m generally not a fan of transitions
or animations unless they are being
used to help “tell the story” of a UI
component - help users understand
what is happening.
84. Transitions should not draw
attention to themselves. Ideally they
should be simple and subtle.
85. For the checkbox, we could do a
very simple transition to animate
the switch from unchecked to
checked - to help users understand
what has happened.
86. We can do this by transitioning the
“left” property as it changes from
unchecked to checked.