前端技术学习之选择器(十一)
2017-01-16 19:27
253 查看
十二,:checked选择器
在表单元素中,单选按钮和复选按钮都有选中和未选中状态。如果有做过尝试就知道,要设置这两个按钮默认样式是比较困难的。而在css3中,可以通过:checked选择器配合其他标签实现自定义样式。而:checked表示的是选中状态。
代码例子:
<!DOCTYPE HTML>
<meta charset="utf-8">
<style type="text/css">
form {
border: 1px solid #ccc;
padding: 20px;
width: 300px;
margin: 30px auto;
}
.wrapper {
margin-bottom: 10px;
}
.box {
display: inline-block;
width: 30px;
height: 30px;
margin-right: 10px;
position: relative;
background: orange;
vertical-align: middle;
border-radius: 100%;
}
.box input {
opacity: 0;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10;/*使input按钮在span的上一层,不加点击区域会出现不灵敏*/
}
.box span {
display: block;
width: 10px;
height: 10px;
border-radius: 100%;
position: absolute;
background: #fff;
top: 50%;
left: 50%;
margin: -5px 0 0 -5px;
z-index: 1;
}
input[type="radio"] + span {
opacity: 0;
}
input[type="radio"]:checked + span {
opacity: 1;
}
</style>
<form action="#">
<div class="wrapper">
<div class="box">
<input type="radio" checked="checked" id="male" name="gender"/>
<span></span>
</div>
<label for="male">男</label>
</div>
<div class="wrapper">
<div class="box">
<input type="radio" id="female" name="gender"/>
<span></span>
</div>
<label for="female">女</label>
</div>
</form>
</body>
</html>
运行效果:
在表单元素中,单选按钮和复选按钮都有选中和未选中状态。如果有做过尝试就知道,要设置这两个按钮默认样式是比较困难的。而在css3中,可以通过:checked选择器配合其他标签实现自定义样式。而:checked表示的是选中状态。
代码例子:
<!DOCTYPE HTML>
<meta charset="utf-8">
<style type="text/css">
form {
border: 1px solid #ccc;
padding: 20px;
width: 300px;
margin: 30px auto;
}
.wrapper {
margin-bottom: 10px;
}
.box {
display: inline-block;
width: 30px;
height: 30px;
margin-right: 10px;
position: relative;
background: orange;
vertical-align: middle;
border-radius: 100%;
}
.box input {
opacity: 0;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 10;/*使input按钮在span的上一层,不加点击区域会出现不灵敏*/
}
.box span {
display: block;
width: 10px;
height: 10px;
border-radius: 100%;
position: absolute;
background: #fff;
top: 50%;
left: 50%;
margin: -5px 0 0 -5px;
z-index: 1;
}
input[type="radio"] + span {
opacity: 0;
}
input[type="radio"]:checked + span {
opacity: 1;
}
</style>
<form action="#">
<div class="wrapper">
<div class="box">
<input type="radio" checked="checked" id="male" name="gender"/>
<span></span>
</div>
<label for="male">男</label>
</div>
<div class="wrapper">
<div class="box">
<input type="radio" id="female" name="gender"/>
<span></span>
</div>
<label for="female">女</label>
</div>
</form>
</body>
</html>
运行效果:
相关文章推荐
- CSS3属性教程与案例分享
- CSS3属性教程与案例分享
- CSS3的calc()使用
- 使用CSS3的appearance属性改变元素的外观
- CSS3美化图片
- CSS3质感分析——表面线性渐变
- 分析选项卡底部内凹圆角制作
- 细说百度图片栏目——图片展示效果
- 8个实用的响应式设计框架
- 20+ Animation Scripts, Libraries and Plugins
- CSS3结合fontawesome字体实现自定义单选框复选框效果
- 新时代编辑神器:Atom
- Mootools 1.2教程(2) DOM选择器
- 如何使用CSS3画出一个叮当猫
- 基于Jquery和CSS3制作数字时钟附源码下载(CSS3篇)
- 浅谈jquery选择器 :first与:first-child的区别
- jquery通过closest选择器修改上级元素的方法
- jQuery选择器简明总结(含用法实例,一目了然)
- jQuery双向列表选择器DIV模拟版
- jQuery表单对象属性过滤选择器实例详解