900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > html改变单选形状 用css实现html中单选框样式改变

html改变单选形状 用css实现html中单选框样式改变

时间:2018-08-03 13:59:29

相关推荐

html改变单选形状 用css实现html中单选框样式改变

我们都知道,input的单选框是一个小圆框,不能直接更改样式。但是我们在很多网页中看到的单选框样式可不仅限于默认的那个样式(看上去没啥新意,也比较丑)。那么,接下来我将介绍下如何实现该功能。

首先,让我们来看下单选框的实现:

在html中的input元素中,将其type属性值设置为radio,即为单选框,此时只需将要设置的单选选项的name属性设置成一致的,即可实现单选功能。

代码实现如下:

要实现单选框样式改变,只需以下几步:

1、首先给出input的type属性为radio,设置相同name。同时用label将相应字段关联上该input属性(当label的for属性值与input的id属性值相同时 ,即可关联。关联上之后,点击字段即可选中相应的单选框)。

html中:

按热门排序

按时间排序

按评价排序

2、设置input的display属性为none,将比较丑的那个框去掉。然后用伪元素添加相应的框。即在label中,用::before添加一个空心圆圈。

div>input{

display: none;

}

div>label{

position: relative;

margin-right: 34px;

}

div>label::before{

display: inline-block;

content: "";

width: 16px;

height: 16px;

border-radius: 50%;

border: 1px solid rgb(219, 219, 219);

margin-right: 6px;

vertical-align: bottom;

}

3、在input单选框在选中的时候,原有before的单选框变为红色实心框,同时添加label::after为白色圆,并将其定位到before的红色实心框中间。从而达到重叠的效果,实现红色圆框+白色圆心的效果。

div>input:checked+label::before{

background-color: rgb(239, 66, 56);

}

div>input:checked+label::after{

display: inline-block;

content: "";

width: 6px;

height: 6px;

border-radius: 50%;

position: absolute;

left: 6px;

bottom: 6px;

background-color: white;

}

是不是挺简单的呢?通过这样的方法还可以设置很多样式呢。

完整代码如下:

Document

div>input{

display: none;

}

div>label{

position: relative;

margin-right: 34px;

}

div>label::before{

display: inline-block;

content: "";

width: 16px;

height: 16px;

border-radius: 50%;

border: 1px solid rgb(219, 219, 219);

margin-right: 6px;

vertical-align: bottom;

}

div>input:checked+label::before{

background-color: rgb(239, 66, 56);

}

div>input:checked+label::after{

display: inline-block;

content: "";

width: 6px;

height: 6px;

border-radius: 50%;

position: absolute;

left: 6px;

bottom: 6px;

background-color: white;

}

按热门排序

按时间排序

按评价排序

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。