修改原生input checkbox样式、radio样式

<!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>原生样式改造</title>
</head>
<style>
    body {
        background-color: #000100;
        color: #fff;
        display: flex;
    }

    .title {
        padding: 10px 0;
    }

    .item {
        padding: 10px 0;
        display: flex;
    }

    .item-label {
        font-size: 20px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #fff;
    }

    .item-value {
        font-size: 20px;
        font-family: Microsoft YaHei;
        color: #00FFFF;
        margin-right: 20px;
    }

    .item-value input[type="checkbox"],
    .item-value input[type="radio"] {
        display: none;
        /* 隐藏原生的多选框 */
    }

    .item-value input[type="checkbox"]+label>i,
    .item-value input[type="radio"]+label>i {
        /* 左侧选框的样式 */
        position: relative;
        display: inline-block;
        width: 20px;
        height: 20px;
        border: 2px solid #00FFFF;
        background: transparent;
        margin-right: 10px;
        vertical-align: middle;
        border-radius: 3px;
        top: -2px;
        box-sizing: border-box;
    }

    .item-value input[type="radio"]+label>i {
        border-radius: 50%;
    }

    .item-value input[type="checkbox"]+label,
    .item-value input[type="radio"]+label {
        /* hover样式 */
        cursor: pointer;
    }

    .item-value input[type="checkbox"]:checked+label,
    .item-value input[type="radio"]:checked+label {
        /* 选中之后label的颜色 */
        color: #FCDE11;
    }

    .item-value input[type="checkbox"]:checked+label>i {
        /* 选中之后选框的样式 */
        background: #FCDE11;
        border: 2px solid #FCDE11;
    }

    .item-value input[type="radio"]:checked+label>i {
        /* 选中之后选框的样式 */
        border: 2px solid #FCDE11;
    }

    .item-value input[type="checkbox"]:checked+label>i::before {
        /* 选中之后选框内对勾的样式 */
        content: '';
        position: absolute;
        width: 6px;
        height: 16px;
        border-bottom: 3px solid #FFFFFF;
        border-right: 3px solid #FFFFFF;
        left: 50%;
        top: 20%;
        transform-origin: center;
        transform: translate(-50%, -30%) rotate(40deg);
        -webkit-transform: translate(-50%, -30%) rotate(40deg);
    }

    .item-value input[type="radio"]:checked+label>i::before {
        /* 选中之后选框内圆点的样式 */
        width: 10px;
        height: 10px;
        content: '';
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        margin: auto;
        background: #FCDE11;
        border-radius: 50%;
    }
</style>

<body>
    <div>
        <h3>checkbox</h3>
        <div class="title">原生</div>
        <div class="item">
            <label for="hei">
                <input name="label3" id="hei" type="checkbox" value="3">
                <span></span>
            </label>
            <label for="bai">
                <input checked name="label4" id="bai" type="checkbox" value="4">
                <span></span>
            </label>
        </div>

        <div class="title">样式改造</div>
        <div class="item">
            <div class="item-label">选项:</div>
            <div class="item-value">
                <input id="ld" name="select" type="checkbox" checked>
                <label for="ld"><i></i>雷达</label>
            </div>
            <div class="item-value">
                <input id="wx" name="select" type="checkbox">
                <label for="wx"><i></i>卫星</label>
            </div>
            <div class="item-value">
                <input id="sb" name="select" type="checkbox">
                <label for="sb"><i></i>色斑</label>
            </div>
        </div>
    </div>

    <div>
        <h3>radio</h3>
        <div class="title">原生</div>
        <div class="item">
            <label for="wu2">
                <input name="label1" id="wu2" type="radio" value="11">
                <span></span>
            </label>
            <label for="you2">
                <input checked name="label2" id="you2" type="radio" value="22">
                <span></span>
            </label>
        </div>

        <div class="title">样式改造</div>
        <div class="item">
            <div class="item-label">选项:</div>
            <div class="item-value">
                <input id="ld2" name="select" type="radio" checked>
                <label for="ld2"><i></i>雷达</label>
            </div>
            <div class="item-value">
                <input id="wx2" name="select" type="radio">
                <label for="wx2"><i></i>卫星</label>
            </div>
            <div class="item-value">
                <input id="sb2" name="select" type="radio">
                <label for="sb2"><i></i>色斑</label>
            </div>
        </div>
    </div>
</body>

</html>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用 CSS 修改 checkboxradio 样式和尺寸后,可能会导致选择框和后面对应的文字无法水平对齐。这是因为原生的选择框和自定义的选择框大小和位置不同,导致文字对齐出现问题。为了解决这个问题,可以采用以下两种方法之一: 1. 使用 `vertical-align` 属性将选择框和文字垂直对齐。示例代码如下: ```css input[type="checkbox"], input[type="radio"] { display: none; } label.checkbox, label.radio { display: inline-block; vertical-align: middle; margin-right: 10px; position: relative; padding-left: 30px; cursor: pointer; } label.checkbox:before, label.radio:before { content: ""; display: inline-block; width: 20px; height: 20px; border: 1px solid #ccc; position: absolute; left: 0; top: 0; border-radius: 4px; transition: all 0.3s ease; } label.checkbox:before { border-radius: 4px; } label.checkbox:after, label.radio:after { content: ""; display: inline-block; width: 10px; height: 10px; position: absolute; left: 4px; top: 4px; border-radius: 2px; transition: all 0.3s ease; opacity: 0; } label.checkbox:hover:before, label.radio:hover:before { border-color: #666; } input[type="checkbox"]:checked + label.checkbox:before, input[type="radio"]:checked + label.radio:before { border-color: #f00; } input[type="checkbox"]:checked + label.checkbox:after, input[type="radio"]:checked + label.radio:after { opacity: 1; } ``` 2. 将选择框和文字放在容器中,并使用 `display: flex; align-items: center;` 将其垂直对齐。示例代码如下: ```css .container { display: flex; align-items: center; } input[type="checkbox"], input[type="radio"] { display: none; } label.checkbox, label.radio { position: relative; padding-left: 30px; cursor: pointer; } label.checkbox:before, label.radio:before { content: ""; display: inline-block; width: 20px; height: 20px; border: 1px solid #ccc; position: absolute; left: 0; top: 0; border-radius: 4px; transition: all 0.3s ease; } label.checkbox:before { border-radius: 4px; } label.checkbox:after, label.radio:after { content: ""; display: inline-block; width: 10px; height: 10px; position: absolute; left: 4px; top: 4px; border-radius: 2px; transition: all 0.3s ease; opacity: 0; } label.checkbox:hover:before, label.radio:hover:before { border-color: #666; } input[type="checkbox"]:checked + label.checkbox:before, input[type="radio"]:checked + label.radio:before { border-color: #f00; } input[type="checkbox"]:checked + label.checkbox:after, input[type="radio"]:checked + label.radio:after { opacity: 1; } ``` 以上两种方法都可以解决选择框和文字无法水平对齐的问题,根据实际情况选择其中一种即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值