将html的radio单选框自定义样式为正方形和对号

将html的radio单选框自定义样式为正方形和对号

背景:

如何能把html的<input type="radio" name="option">改成自定义的样式呢?比如想要把他变成正方形,选中的时候是对号。默认的样式太丑了

默认样式:
在这里插入图片描述
自定义样式:
在这里插入图片描述

实现代码
<!DOCTYPE html>
<html>
<head>
<style>
input[type="radio"] {
     
  display: none; 
}  
  

.square-radio {
     
    display: flex;
  position: relative;  
  width: 20px;  
  height: 20px;  
  border: 2px solid #333; 
  cursor: pointer; 
}  
  
 
.square-radio::after {
     
  content: "✓";  
  position: absolute;  
  top: 50%;  
  left: 50%;  
  transform: translate(-50%, -50%);   
  font-size: 14px;  
  color: #333;  
  display: none;
}  
   
  
input[type="radio"]:checked + .square-radio::after {
     
  color: #2196F3;  
  display: block;
}
</style>
</head>
<body>



<div class="box">
    性别:
    <label><input type="radio" name="option">  
        <span class="square-radio"></span>  
    </label>
    <label><input type="radio" name="option">  
        <span class="square-radio"></span>  
    </label>
</div>
</body>
</html>

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-14 11:08:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-14 11:08:01       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-14 11:08:01       82 阅读
  4. Python语言-面向对象

    2023-12-14 11:08:01       91 阅读

热门阅读

  1. 【RabbitMQ高可用集群架构】

    2023-12-14 11:08:01       56 阅读
  2. 什么是DNS解析?DNS解析的过程是什么样的?

    2023-12-14 11:08:01       74 阅读
  3. 装饰器模式

    2023-12-14 11:08:01       58 阅读
  4. 枚举+链对缓存实现以读取为主的请求接口

    2023-12-14 11:08:01       52 阅读
  5. 如何避免缓存穿透、缓存击穿、缓存雪崩?

    2023-12-14 11:08:01       63 阅读
  6. CUDA锁页内存的使用

    2023-12-14 11:08:01       61 阅读
  7. 跨线程保持上下文

    2023-12-14 11:08:01       52 阅读
  8. linux的命令和vi的使用

    2023-12-14 11:08:01       55 阅读
  9. xtu oj 1271 color

    2023-12-14 11:08:01       53 阅读
  10. Ceph入门到精通-TCP和UDP相关的指标

    2023-12-14 11:08:01       53 阅读
  11. ceph-deploy 遇到的使用问题

    2023-12-14 11:08:01       58 阅读