
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to create a "toggle switch" (on/off button) with CSS?
To create a toggle switch with CSS, the code is as follows −
Example
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> .switch { position: relative; display: inline-block; width: 60px; height: 34px; } .switch input { opacity: 0; width: 0; height: 0; } span { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; -webkit-transition: .4s; transition: .4s; } span:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px; background-color: white; -webkit-transition: .4s; transition: .4s; } input:checked + span { background-color: rgb(85, 21, 138); } input:focus + span { box-shadow: 0 0 1px rgb(255, 77, 211); } input:checked + span:before { transform: translateX(26px); } span.round { border-radius: 34px; } span.round:before { border-radius: 50%; } </style> </head> <body> <h1>Toggle Switch Example</h1> <label class="switch"> <input type="checkbox"> <span class="round"></span> </label> <label class="switch"> <input type="checkbox" checked> <span class="round"></span> </label> </body> </html>
Output
The above code will produce the following output −
- Related Articles
- On/Off Toggle Button Switch in Tkinter
- How to create toggle switch by using HTML and CSS?
- How to create a Tkinter toggle button?
- How to create a Toggle Button in JavaFX?
- How to toggle between a like/dislike button with CSS and JavaScript?
- How to create a "button group" with CSS?
- How to create a "more" button with CSS?
- How to create a search button with CSS?
- How to create a vertical "button group" with CSS?
- How to create a split button dropdown with CSS?
- Create a Button Group with CSS
- How to create a "scroll back to top" button with CSS?
- Create a Bordered Button Group with CSS
- Create a Vertical Button Group with CSS
- How to create an off-canvas menu with CSS and JavaScript?

Advertisements