前端修改radio单选框的样式

1.效果一

在这里插入图片描述
代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>改变单选框样式</title>
    <style>
    label{     
        position: relative;     
        margin: 0 20px;
    }    
    label::before{      
        position: absolute;   
        left: -20px;     
        width: 16px;     
        height: 16px;     
        border: 1px solid #399bfd;      
        border-radius: 50%;      
        content: "";      
        box-sizing: border-box;
    }
    input:checked+label::before{      
        background-color: white;      
        border: 5px solid #399bfd;
    }
    input[type="radio"]{     
        display: none;
    }
    </style>
</head>
<body>
    <input type="radio" name="1" id="item1"><label for="item1">选项一</label>
    <br>
    <input type="radio" name="1" id="item2"><label for="item2">选项二</label>
</body>
</html>

参考文章

2.效果二

在这里插入图片描述
代码:和效果一只差在了如下部分(可直接用效果一的代码,然后改一下这个地方就可以了)
在这里插入图片描述

3.效果三

在这里插入图片描述
代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>改变单选框样式</title>
    <style>
    input[type="radio"] {
        position: absolute; /*绝对定位*/
        clip: rect(0,0,0,0); /*裁剪图像*/
    }
    label {
        display: inline-flex; /*将对象作为内联块级弹性伸缩盒显示*/
        flex-direction: row; /*主轴为水平方向,起点在左端*/
        align-items: center; /*垂直居中*/
    }
    /*紧邻lable标签的单选按钮创建伪元素*/
    input[type="radio"] + label::before {
        content: ''; /*插入内容为空*/
        display: inline-block; /*融合行内于块级*/
        width: 16px;
        height: 16px;
        border-radius: 10px; /*设置圆角*/
        border: 1px solid #666666;
        margin-right: 3px;
    }
    /*紧邻lable标签选中的单选按钮创建伪元素*/
    input[type="radio"]:checked + label::before {
        background-clip: content-box; /*背景裁剪到内容框*/
        background-color: #BB1E19;
        width: 8px;
        height: 8px;
        padding: 4px;
        border: 1px solid #BB1E19;
    }

    </style>
</head>
<body>
    <input type="radio" name="1" id="item1"><label for="item1">选项一</label>
    <br>
    <input type="radio" name="1" id="item2"><label for="item2">选项二</label>
</body>
</html>

参考文章

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值