Toggle Switch Template

A stylish and accessible toggle switch for enabling or disabling options with a single click.

This component provides a more intuitive and visually appealing alternative to a traditional checkbox for binary choices, such as toggling dark mode or notifications.

Screenshot of an animated on/off toggle switch control.

Get Source Code Preview

About the Toggle Switch

Toggle switches offer a clean and modern way to present a simple on/off choice. This template uses a clever CSS technique to style a semantic HTML checkbox, resulting in a beautiful switch that is fully accessible and requires no JavaScript for its basic functionality.

Features

Code Breakdown

The structure consists of a checkbox input and an associated label. The checkbox itself is visually hidden, but remains functional in the background. The label is styled to look like the track of the switch.

The "toggle" part happens with the :checked pseudo-class and the adjacent sibling selector (+). When the checkbox is checked, the rule .toggle-checkbox-1:checked + .toggle-label-1 triggers, changing the background color of the label. Similarly, a rule targeting the label's ::after pseudo-element, which serves as the handle, uses a transform to slide the handle to the "on" position.

Code

Here is the complete HTML and CSS for the accessible toggle switch.

View Output