The first is the idea:
We use the tag to achieve this effect.
The selected and unselected features of the checkbox correspond to the on and off of the switch
on:on off:off
<label for="ck2"> <input type="checkbox" id="ck2"> <span>未选中,则关闭开关</span> </label> <br> <label for="ck1"> <input type="checkbox" id="ck1" checked> <span>选中,则打开开关</span> </label>
Effect:
(Recommended tutorial: CSS Introduction Tutorial)
Start drawing the sketch of the off and on states
Here I will explain, using position to achieve positioning. Students who don’t understand can open MDN to view relevant knowledge
<P>off状态草图</P> <div class="toggle"> <div class="cookie"></div> </div> <br> <P>on状态草图</P> <div class="toggle2"> <div class="cookie2"></div> </div> .toggle{ display:inline-block; position:relative; height:25px; width:50px; border-radius:4px; background:#CC0000; } .cookie{ position:absolute; left:2px; top:2px; bottom:2px; width:50%; background:rgba(230,230,230,0.9); border-radius:3px; } .toggle2{ display:inline-block; position:relative; height:25px; width:50px; padding:2px; border-radius:4px; background:#66CC33; } .cookie2{ position:absolute; right:2px; top:2px; bottom:2px; width:50%; background:rgba(230,230,230,0.9); border-radius:3px; }
Effect:
Then we put these two sketches into the label
<label for="ck4"> <input type="checkbox" id="ck4"> <div class="toggle"> <div class="cookie"></div> </div> </label> <br> <label for="ck3"> <input type="checkbox" id="ck3" checked> <div class="toggle2"> <div class="cookie2"></div> </div> </label>
Effect:
Combined label and checkbox to organize and optimize css
<label for="ck5"> <input type="checkbox" id="ck5"> <div class="toggle-finish"> <div class="cookie-finish"></div> </div> </label> <br> <label for="ck6"> <input type="checkbox" id="ck6" checked> <div class="toggle-finish"> <div class="cookie-finish"></div> </div> </label> .toggle-finish{ cursor:pointer; display:inline-block; position:relative; height:25px; width:50px; border-radius:4px; background:#CC0000; } .cookie-finish{ position:absolute; left:2px; top:2px; bottom:2px; width:50%; background:rgba(230,230,230,0.9); border-radius:3px; } input:checked + .toggle-finish{ background:#66CC33; } input:checked + .toggle-finish .cookie-finish{ left:auto; right:2px; }
Effect:
So far, the function of a switch has been basically implemented. Remember to hide the input.
Recommended related video tutorials: css video tutorial
The above is the detailed content of How to achieve switch effect in css. For more information, please follow other related articles on the PHP Chinese website!

I recently found a solution to dynamically update the color of any product image. So with just one of a product, we can colorize it in different ways to show

In this week's roundup, Lighthouse sheds light on third-party scripts, insecure resources will get blocked on secure sites, and many country connection speeds

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

The document head might not be the most glamorous part of a website, but what goes into it is arguably just as important to the success of your website as its

What's happening when you see some JavaScript that calls super()?.In a child class, you use super() to call its parent’s constructor and super. to access its

JavaScript has a variety of built-in popup APIs that display special UI for user interaction. Famously:

I was chatting with some front-end folks the other day about why so many companies struggle at making accessible websites. Why are accessible websites so hard

There is an HTML attribute that does exactly what you think it should do:


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor