Today, I wanted to quickly share how I built Kelp’s toggle switch component with a single HTML attribute and CSS (no JavaScript required).

Let’s dig in!

The [role="switch"] attribute

The [role="switch"] attribute is functionality identical to [role="checkbox"] (the implicit role a [type="checkbox"] element has), except that it conveys an on/off state instead of checked/unchecked.

<label for="agree">
<input
type="checkbox"
id="agree"
>
I'm a standard checkbox
</label>

<label for="switch">
<input
type="checkbox"
id="switch"
role="switch"
>
I'm a switch
</label>

On it’s own, it changes nothing about how a checkbox looks or behaves.

I’m …

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help