css3修改美化radio、checkbox的默认样式的简单方案
发表于:2024-10-10 18:28:00浏览:2178次
现在前端页面效果要求特别高,页面中表单的默认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的美化。
推荐文章
- 利用微软的Office Online实现Office文档在线预览功能
- PHP判断网站的访问来源是否是蜘蛛
- PHP 8.5于2025年11月20日正式发布:十大核心改进
- MySQL各类数据类型的最大长度与范围限制
- 微软发布首个 Windows 11 ISO 版本 新的浏览器大战又要开始?
- 最新PHP 7.4.32, PHP8.0.24 & PHP8.1.11三个分支发布了新版本
- HTML5音频播放标签介绍及实现简单的音频播放器代码
- PHP根据昵称或者姓名自动生成文字头像(图片)的方法
- PHP实现今天、昨天 、本周、上周、本月、上月、本季度等方法
- 微信浏览器或手机浏览器字体大小调整 导致H5页面布局错乱解决方案

