CSS3自定义复选框和单选框

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS3自定义复选框和单选框</title>
    <style>
        /* 基础样式 */
        body {
            font-family: 'Arial', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
            color: #333;
        }
        
        h1 {
            color: #2c3e50;
            text-align: center;
            margin-bottom: 30px;
        }
        
        .container {
            display: flex;
            flex-wrap: wrap;
            gap: 30px;
            justify-content: center;
        }
        
        .section {
            background: #f9f9f9;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            width: 300px;
        }
        
        h2 {
            color: #3498db;
            margin-top: 0;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        
        /* 自定义复选框样式 */
        .checkbox-container {
            display: block;
            position: relative;
            padding-left: 35px;
            margin-bottom: 15px;
            cursor: pointer;
            font-size: 16px;
            user-select: none;
        }
        
        /* 隐藏默认复选框 */
        .checkbox-container input {
            position: absolute;
            opacity: 0;
            cursor: pointer;
            height: 0;
            width: 0;
        }
        
        /* 自定义复选框 */
        .checkmark {
            position: absolute;
            top: 0;
            left: 0;
            height: 22px;
            width: 22px;
            background-color: #eee;
            border-radius: 4px;
            transition: all 0.3s;
        }
        
        /* 悬停效果 */
        .checkbox-container:hover input ~ .checkmark {
            background-color: #ccc;
        }
        
        /* 选中状态 */
        .checkbox-container input:checked ~ .checkmark {
            background-color: #3498db;
        }
        
        /* 创建勾选标记 */
        .checkmark:after {
            content: "";
            position: absolute;
            display: none;
        }
        
        /* 显示勾选标记 */
        .checkbox-container input:checked ~ .checkmark:after {
            display: block;
        }
        
        /* 勾选标记样式 */
        .checkbox-container .checkmark:after {
            left: 8px;
            top: 4px;
            width: 5px;
            height: 10px;
            border: solid white;
            border-width: 0 2px 2px 0;
            transform: rotate(45deg);
        }
        
        /* 自定义单选框样式 */
        .radio-container {
            display: block;
            position: relative;
            padding-left: 35px;
            margin-bottom: 15px;
            cursor: pointer;
            font-size: 16px;
            user-select: none;
        }
        
        /* 隐藏默认单选框 */
        .radio-container input {
            position: absolute;
            opacity: 0;
            cursor: pointer;
        }
        
        /* 自定义单选框 */
        .radio-btn {
            position: absolute;
            top: 0;
            left: 0;
            height: 22px;
            width: 22px;
            background-color: #eee;
            border-radius: 50%;
            transition: all 0.3s;
        }
        
        /* 悬停效果 */
        .radio-container:hover input ~ .radio-btn {
            background-color: #ccc;
        }
        
        /* 选中状态 */
        .radio-container input:checked ~ .radio-btn {
            background-color: #3498db;
        }
        
        /* 创建内圆 */
        .radio-btn:after {
            content: "";
            position: absolute;
            display: none;
        }
        
        /* 显示内圆 */
        .radio-container input:checked ~ .radio-btn:after {
            display: block;
        }
        
        /* 内圆样式 */
        .radio-container .radio-btn:after {
            top: 7px;
            left: 7px;
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: white;
        }
        
        /* 禁用状态 */
        .checkbox-container input:disabled ~ .checkmark,
        .radio-container input:disabled ~ .radio-btn {
            background-color: #e0e0e0;
            cursor: not-allowed;
        }
        
        /* 链接样式 */
        .footer {
            text-align: center;
            margin-top: 40px;
            padding: 20px;
            font-size: 14px;
        }
        
        .footer a {
            color: #3498db;
            text-decoration: none;
        }
        
        .footer a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <h1>CSS3自定义复选框和单选框</h1>
    
    <div class="container">
        <div class="section">
            <h2>自定义复选框</h2>
            
            <label class="checkbox-container">
                <input type="checkbox" checked="checked">
                <span class="checkmark"></span>
                默认选中
            </label>
            
            <label class="checkbox-container">
                <input type="checkbox">
                <span class="checkmark"></span>
                未选中
            </label>
            
            <label class="checkbox-container">
                <input type="checkbox" disabled>
                <span class="checkmark"></span>
                禁用状态
            </label>
        </div>
        
        <div class="section">
            <h2>自定义单选框</h2>
            
            <label class="radio-container">
                <input type="radio" name="radio" checked="checked">
                <span class="radio-btn"></span>
                选项一
            </label>
            
            <label class="radio-container">
                <input type="radio" name="radio">
                <span class="radio-btn"></span>
                选项二
            </label>
            
            <label class="radio-container">
                <input type="radio" name="radio" disabled>
                <span class="radio-btn"></span>
                禁用选项
            </label>
        </div>
    </div>
    
    <div class="footer">

    </div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值