What does this CSS snippet do?
This CSS snippet creates a custom toggle switch using a hidden checkbox and a styled label
How does it work:
- Checkbox is hidden but still functional.
- Label acts as the switch track, styled with rounded edges and a default gray background.
- When checked, the label turns green, and:
- The knob (
::after
) slides to the right. - A checkmark icon (
::before
) appears using FontAwesome.
- The knob (
- Transitions add smooth movement for the toggle knob.
- An optional span element can display a label or status next to the switch.
The interaction relies on the :checked + label
CSS selector to update the visual state based on whether the checkbox is selected.
CSS
.demo-switch input[type=checkbox] {
visibility: hidden;
height: 0;
width: 0;
}
.demo-switch input[type=radio] {
visibility: hidden;
height: 0;
width: 0;
}
.demo-switch label {
position: relative;
display: block;
cursor: pointer;
background-color: #CCCCCC;
border-radius: 100px;
width: 66px;
height: 30px;
}
.demo-switch input:checked + label {
background-color: #63AE6F;
}
.demo-switch input:checked + label::after {
left: calc(100% - 4px);
transform: translateX(-100%);
}
.demo-switch input:checked + label::before {
font: 18px/1 FontAwesome;
position: absolute;
content: '\f00c';
top: 6px;
left: 10px;
color: #FFFFFF;
font-weight: bold;
}
.demo-switch label::after {
content: '';
position: absolute;
top: 4px;
left: 6px;
width: 22px;
height: 22px;
background-color: #FFFFFF;
border-radius: 90px;
transition: 0.3s;
}
.demo-switch span {
display: inline-block;
position: absolute;
width: 160px;
top: 2px;
left: 90px;
text-transform: uppercase;
font-size: 12px;
letter-spacing: 1px;
text-align: center;
border-top: 1px dashed #CCCCCC;
border-bottom: 1px dashed #CCCCCC;
padding: 4px 0;
}
.demo-switch span:hover {
color: #FB9338;
}
HTML
<div class="demo-content">
<div class="demo-switch">
<input type="checkbox" name="checkbox-01" id="demo-checkbox-01">
<label for="demo-checkbox-01"><span>First Item</span></label>
<input type="checkbox" name="checkbox-02" id="demo-checkbox-02">
<label for="demo-checkbox-02"><span>Second Item</span></label>
</div>
</div>
Arrow at the bottom
Arrow at the bottom with a utf-8 character
Arrow at the top of a container
Aside
Background color
Blackboard design
Breadcrumbs design
Calendar design
Checkboxes as circular buttons
Checkboxes as toggle switches
Cursor properties
Circular image with a shadow effect
Font Awesome pulled icons
Gallery picture frame with a price ribbon
Google pie chart
Google web fonts
Last child and not last child element
Letter spacing
List items with a Font Awesome icon
Neon text
Neon text with a flickering effect
Notebook paper background
Radio buttons as circular buttons
Radio buttons as rating stars
Remove the glow effect from an input field
Search bar with categories
Track (audio/video subtitles)
Web fonts
Whiteboard design