纯css--开关按钮

本文详细介绍了如何仅使用CSS创建具有完整交互效果的开关按钮,涵盖了样式设置、伪类应用以及过渡动画等方面,帮助读者掌握纯CSS实现开关按钮的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTML 结构:

<div class="toggle-button-wrapper">
    <input type="checkbox" id="toggle-button" name="switch">
    <label for="toggle-button" class="button-label">
        <span class="circle"></span>
        <span class="text on">开</span>
        <span class="text off">关</span>
    </label>
</div>

css样式:

#toggle-button{
            display: none;
        }
        .button-label{
            position: relative;
            display: inline-block;
            width: 54px;
            height: 24px;
            border: 1px solid #d2d2d2;
            background-color: #fff;
            border-radius: 27px;
            overflow: hidden;
        }
        .circle{
            position: absolute;
            top: 50%;
            left: 0;
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background-color: #d2d2d2;
            transform: translateY(-50%);
        }
        .button-label .text {
            line-height: 24px;
            font-size: 12px;
        }
        .on {
            color: #fff;
            display: none;
            text-indent: 8px;
        }
        .off {
            color:#999;
            display: inline-block;
            text-indent: 32px;
        }
        .button-label .circle{
            left: 4px;
            transition: all 0.3s;
        }
       #toggle-button:checked + label.button-label .circle {
            left: 30px;
            background-color:#fff;
        }
       #toggle-button:checked + label.button-label .on{
            display: inline-block;
        }
        #toggle-button:checked + label.button-label .off{
            display: none;
        }


        #toggle-button:checked + label.button-label{
            background-color: #5FB878;
            border-color: #5FB878;
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值