栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Html/CSS > CSS教程

css3实现radio选择效果

CSS教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

css3实现radio选择效果

该手记通过简单教程,实现如图所示的radio单选样式效果

html结构,div中包含一个radio单选框与一个指向该单选框的label标签

    
    

初始化相关结构样式,设置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;
}

整体代码示例




    
    
    Examples
    
    
    
    


.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;
}




    
    


    
    


    
    


    
    


    
    


其他相关手记:
通过css3实现开关选择按钮
通过css3实现checkbox选择样式

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/243069.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号