首页 > Web开发 > 详细

css3更改input单选和多选的样式

时间:2017-01-03 13:27:35      阅读:267      评论:0      收藏:0      [点我收藏+]

在项目开发中我们经常会遇到需要更改input单选和多选样式的情况,今天就给大家介绍一种简单改变input单选和多选样式的办法。

在这之前先简单介绍一下:before伪类

:before 选择器向选定的元素前插入内容。使用content 属性来指定要插入的内容(content是必须的哦)。

相信这并不难以理解,接下来我们先理解思路:

(1)首先在html用label为 input 元素定义标记,也就是当你点击label标签时相应的input也会选中或取消

(2)接下来就是写css了,将input隐藏,只需要在label之前加入你的样式就好了,可以是图片也可以是自己画的圆圈,下面的这个例子是我写的圆,当然也可以替换成背景图。

input[type="radio"]+label:before是未选中状态的样式
input[type="radio"]:checked+label:before是选中状态的样式
<input type="radio" id="nationality1"><label for="nationality1">中国</label></div>
input[type="radio"]{
    display:none;
}
input[type="radio"]+label{
    position: relative;
}
input[type="radio"]+label:before{
    content: "";
    width:25px;
    height:25px;
    background: #ffffff;
    border:2px solid $gray;
    position: absolute;
    bottom:3px;
    left: -35px;
    border-radius: 50%;
}                                                   
input[type="radio"]:checked+label:before
{ content: ""; width: 25px; height: 25px; background: #34c0f5; position: absolute; bottom:3px; left: -35px; z-index: 99; border-radius: 50%; }

把radio替换成checkbox就是多选的啦。

注:隐藏和伪类定位是关键

css3更改input单选和多选的样式

原文:http://www.cnblogs.com/xmoomoo/p/6203027.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!