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

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

背景:

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

默认样式:

自定义样式:

实现代码
bash 复制代码
<!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>
相关推荐
a1117763 分钟前
水体渲染系统(html开源)
前端·开源·threejs·水体渲染
程序员小李白29 分钟前
CSS 盒子模型
前端·css·html
Zzz不能停37 分钟前
单行 / 多行文本显示省略号(CSS 实现)
前端·css
xiaoxue..37 分钟前
TailwindCSS:从“样式民工”到“UI乐高大师”的逆袭
前端·css·ui
Anastasiozzzz2 小时前
阿亮随手记:动态条件生成Bean
java·前端·数据库
Highcharts.js3 小时前
数据之美:用Highcharts打造专业级弧线图
javascript·数据库·highcharts·图表开发·弧线图
mclwh3 小时前
关于React-Konva 报:Text components are not supported....错误的问题
前端·react.js
SuperEugene3 小时前
错误处理与 try/catch:真实项目里应该捕什么错?
前端·javascript·面试
Amumu121384 小时前
CSS引入方式
前端·css
我是Superman丶4 小时前
【Demo】✋ 数字手势识别 Html
前端·html