Creating a toggle switch with just CSS | Go Make Things
gomakethings.com·1d
🎨Modern CSS
Preview
Report Post

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...