继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

css3实现radio选择效果

朽梨
关注TA
已关注
手记 7
粉丝 8
获赞 282

该手记通过简单教程,实现如图所示的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选择样式

打开App,阅读手记
12人推荐
发表评论
随时随地看视频慕课网APP

热门评论

不错不错····终于可以抛弃默认的样式了,哈哈,大赞

查看全部评论