Settings Toggle Card

Provide users with clear, binary choices for application settings or preferences with this stylish toggle switch card.

This is an ideal component for any settings page. It presents an option with a title and description, and uses an accessible, animated switch to control its on/off state, all without requiring any JavaScript.

Screenshot of a list of settings options, each with a toggle switch.

Get Source Code Preview

About this Component

The core of this component is a visually styled toggle switch that is built using only HTML and CSS. It's an enhancement of a standard checkbox input, making it more intuitive and visually pleasing. The component is fully accessible to keyboard and screen reader users.

Features

Code Breakdown

HTML Structure

Each setting is a div with the class .stc-card. A label for the title is explicitly linked to the id of its corresponding input. The toggle switch itself is a second label wrapping a visually-hidden input type="checkbox" and a span element which acts as the visual part of the switch (the slider).

CSS Styling

The toggle switch's functionality lies in the use of the :checked pseudo-class and the adjacent sibling selector (+). The input itself is hidden with opacity: 0. The visual element (.stc-toggle-slider) is styled to look like a switch. When the input is checked (input:checked + .stc-toggle-slider), the CSS changes the background color of the slider. The sliding circle is a pseudo-element (::before) on the slider. When the input is checked, its transform: translateX(20px) property is animated, creating the smooth sliding effect.

Code

Here is the complete code. You can easily build a settings page by duplicating the li element for each option you need to provide.

View Output