input单选框和复选框改变自带的颜色和背景色

这篇文章展示了如何使用CSS来改变HTML中的单选框和复选框的默认样式,包括设置背景颜色、边框、大小以及添加自定义选中图标。

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

单选框:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 300px;
            height: 300px;
            margin: 100px auto;
        }
        input[type="radio"]::before {
            position: relative;
            content: "";
            top: -1px;
            left: -1px;
            width: 17px;
            height: 17px;
            display: block;
            border-radius: 50%;
            background-color: #fff;
            border: 1px solid #6b4bff;
            z-index: 5;
        }
        input[type="radio"]:checked::after {
            position: relative;
            content: "";
            bottom: 15px;
            left: 4px;
            width: 9px;
            height: 9px;
            display: block;
            border-radius: 50%;
            visibility: visible;
            background-color: #6b4bff;
            z-index: 6;
        }
    </style>
</head>
<body>
    <div class="box">
        <input type="radio">
    </div>
</body>
</html>

效果图:

复选框:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 300px;
            height: 300px;
            margin: 100px auto;
        }
        input[type=checkbox]{
            cursor: pointer;
            position: relative;
            width: 15px;
            height: 15px;
            font-size: 14px;
        }
        input[type=checkbox]::after{
            position: absolute;
            top: 0;
            border: none;
            /* background-color: #ff670c; */
            color: #fff;
            width: 15px;
            height: 15px;
            display: inline-block;
            visibility: visible;
            padding-left: 0px;
            text-align: center;
            content: ' ';
            border-radius: 1px
        }       
        input[type=checkbox]:checked::after{
            background-color: #6b4bff;
            border-color: #6b4bff;
            content: "✓";
            font-size: 12px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="box">
        <input type="checkbox">
        <input type="checkbox">
    </div>
</body>
</html>

效果图:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值