css3修改美化radio、checkbox的默认样式的简单方案
发表于:2024-10-10 18:28:00浏览:2350次
现在前端页面效果要求特别高,页面中表单的默认input组件样式显然不能满足广大用户的体验需求,之前在开发开发项目中刚好有相关的需求,在此特地整理下修改radio、CheckBox样式的方法。
具体原理:是使用原生的checkbox或input标签,在其后面设置相关联的label元素。给input元素设置为透明,然后通过定位让用户看到的是
利用css3伪元素实现样式修改,先看效果图。

具体HTML代码如下:
<div class="mbui-form">
<label class="mbui-form-label">工作类型</label>
<div class="mbui-form-radio">
<input class="mbui-input-radio" type="radio" name="labor_type" id="labor_radio1" />
<label for="labor_radio1">案头工作</label>
<input class="mbui-input-radio" type="radio" name="labor_type" id="labor_radio2" />
<label for="labor_radio2">外勤工作</label>
</div>
</div>
<div class="mbui-form">
<label class="mbui-form-label">工作类型</label>
<div class="mbui-form-checkbox">
<input class="mbui-input-checkbox" type="checkbox" name="labor_types" id="labor_checkbox1" />
<label for="labor_checkbox1">外勤工作</label>
<input class="mbui-input-checkbox" type="checkbox" name="labor_types" id="labor_checkbox2" />
<label for="labor_checkbox2">外勤工作</label>
</div>
</div>
具体CSS代码如下:
/*radio*/
.mbui-form-radio {
width: 100%;
border: 0;
font-size: 16px;
color: #212121;
padding: 14px 12px 14px 120px;
background: #FFF;
line-height: 1.4
}
.mbui-form-radio .mbui-input-radio {
width: 19px;
height: 19px;
appearance: none;
position: relative;
}
.mbui-form-radio .mbui-input-radio:before {
content: '';
width: 19px;
height: 19px;
border: 1px solid #888888;
display: inline-block;
border-radius: 50%;
vertical-align: bottom;
}
.mbui-form-radio .mbui-input-radio:checked:before {
content: '';
width: 19px;
height: 19px;
border: 1px solid #267EF0;
background: #267EF0;
display: inline-block;
border-radius: 50%;
vertical-align: bottom;
}
.mbui-form-radio .mbui-input-radio:checked:after {
content: '';
width: 10px;
height: 5px;
border: 2px solid white;
border-top: transparent;
border-right: transparent;
text-align: center;
display: block;
position: absolute;
top: 6px;
left: 5px;
vertical-align: bottom;
transform: rotate(-45deg);
}
.mbui-form-radio .mbui-input-radio+label {
line-height: 19px;
display: inline-block;
margin-left: 5px;
margin-right: 15px;
color: #666;
}
/*checkbox*/
.mbui-form-checkbox {
width: 100%;
border: 0;
font-size: 16px;
color: #212121;
padding: 14px 12px 14px 120px;
background: #FFF;
line-height: 1.4
}
.mbui-form-checkbox .mbui-input-checkbox {
width: 19px;
height: 19px;
appearance: none;
position: relative;
}
.mbui-form-checkbox .mbui-input-checkbox:before {
content: '';
width: 19px;
height: 19px;
border: 1px solid #888888;
display: inline-block;
border-radius: 3px;
vertical-align: bottom;
}
.mbui-form-checkbox .mbui-input-checkbox:checked:before {
content: '';
width: 19px;
height: 19px;
border: 1px solid #267EF0;
background: #267EF0;
display: inline-block;
border-radius: 3px;
vertical-align: bottom;
}
.mbui-form-checkbox .mbui-input-checkbox:checked:after {
content: '';
width: 10px;
height: 5px;
border: 2px solid white;
border-top: transparent;
border-right: transparent;
text-align: center;
display: block;
position: absolute;
top: 6px;
left: 5px;
vertical-align: middle;
transform: rotate(-45deg);
}
.mbui-form-checkbox .mbui-input-checkbox+label {
line-height: 19px;
display: inline-block;
margin-left: 5px;
margin-right: 15px;
color: #666;
}
该方案充分借助了CSS3的优势,无需使用js和图片,仅用纯CSS3就可以实现对radio、checkbox的美化。
推荐文章
- PHP中的word文档生成与处理库PHPWord的基本使用
- linux环境下,Composer安装项目时报错:Do not run Composer as root/super user!
- 开源OA办公系统 — 勾股OA 5.6.8 新春版发布,企业办公的卓越选择
- 图片变形处理,可设置CSS属性object-fit: cover完美解决
- MySQL各类数据类型的最大长度与范围限制
- 如何在gitee上提交Pull Request,给他人的项目贡献自己的代码
- phpword 使用TemplateProcessor方式实现在word模板中动态插入表格
- 谷歌发布全新操作系统chromeOS Flex首个稳定版,可用于 PC 和 Mac
- linux服务器定时任务crontab命令用法详解
- 最美的国产操作系统:深度操作系统 deepin 20.5 发布

