该手记通过简单教程,实现如图所示的radio单选样式效果
html结构,div中包含一个radio单选框与一个指向该单选框的label标签
<div class='radio-check'>
<input type='radio' name='test' id='test1'/>
<label for='test1' class>苹果</label>
</div>
初始化相关结构样式,设置div定位为relative,radio与label为absolute定位
.radio-check {
position: relative;
height: 35px;
}
.radio-check > input {
position: absolute;
left: 0;
top: 0;
width: 20px;
height:20px;
opacity: 0;
}
.radio-check > label {
position: absolute;
left: 30px;
line-height: 20px;
top: 0px;
}
通过伪类::before与::after,实现选中效果
初始化before与after状态
.radio-check > label:before {
content: '';
position: absolute;
left: -30px;
top: 0px;
display: inline-block;
width: 20px;
height: 20px;
border-radius: 50%;
border: 1px solid #ddd;
transition: all 0.3s ease;
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
}
.radio-check > label:after {
content: '';
position: absolute;
left: -30px;
top: 0px;
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
margin-top: 6px;
margin-left: 6px;
transition: all 0.3s ease;
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
}
利用radio选中状态的:checked伪类选择器与兄弟选择器,实现选中效果
.radio-check input[type='radio']:checked + label:before {
border-color: #4cd764;
}
.radio-check input[type='radio']:checked + label:after {
background: #4cd764;
}
整体代码示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" name="viewport"/>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<style>
.radio-check {
position: relative;
height: 35px;
}
.radio-check > input {
position: absolute;
left: 0;
top: 0;
width: 20px;
height:20px;
opacity: 0;
}
.radio-check > label {
position: absolute;
left: 30px;
line-height: 20px;
top: 0px;
}
.radio-check > label:before {
content: '';
position: absolute;
left: -30px;
top: 0px;
display: inline-block;
width: 20px;
height: 20px;
border-radius: 50%;
border: 1px solid #ddd;
transition: all 0.3s ease;
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
}
.radio-check > label:after {
content: '';
position: absolute;
left: -30px;
top: 0px;
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
margin-top: 6px;
margin-left: 6px;
transition: all 0.3s ease;
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
}
.radio-check input[type='radio']:checked + label:before {
border-color: #4cd764;
}
.radio-check input[type='radio']:checked + label:after {
background: #4cd764;
}
</style>
<body>
<div class='radio-check'>
<input type='radio' name='test' id='test1'/>
<label for='test1' class>苹果</label>
</div>
<div class='radio-check'>
<input type='radio' name='test' id='test2'/>
<label for='test2' class>橘子</label>
</div>
<div class='radio-check'>
<input type='radio' name='test' id='test3'/>
<label for='test3' class>梨</label>
</div>
<div class='radio-check'>
<input type='radio' name='test' id='test4'/>
<label for='test4' class>香蕉</label>
</div>
<div class='radio-check'>
<input type='radio' name='test' id='test5'/>
<label for='test5' class>菠萝</label>
</div>
</body>
</html>
其他相关手记:
通过css3实现开关选择按钮
通过css3实现checkbox选择样式
点击查看更多内容
12人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦