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

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

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

代码实现如下:

<input type="radio" name="gender" value="male">男
<input type="radio" name="gender" value="female">女

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

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

html中:
       <div>
<input type="radio" name="paixu" id="paixu1" checked>
<label for="paixu1" style="cursor:pointer">按热门排序</label>
<input type="radio" name="paixu" id="paixu2">
<label for="paixu2" style="cursor:pointer">按时间排序</label>
<input type="radio" name="paixu" id="paixu3">
<label for="paixu3" style="cursor:pointer">按评价排序</label>
</div>

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

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

完整代码如下:

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
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; }
</style>
</head>
<body>
<div>
<input type="radio" name="paixu" id="paixu1" checked>
<label for="paixu1" style="cursor:pointer">按热门排序</label>
<input type="radio" name="paixu" id="paixu2">
<label for="paixu2" style="cursor:pointer">按时间排序</label>
<input type="radio" name="paixu" id="paixu3">
<label for="paixu3" style="cursor:pointer">按评价排序</label>
</div>
</body> </html>

  

 

用css实现html中单选框样式改变的更多相关文章

  1. 复选框、单选框样式自定义(https://www.cnblogs.com/freedom-feng/p/11346396.html)

    复选框.单选框样式自定义(https://www.cnblogs.com/freedom-feng/p/11346396.html)复选框html内容如下:<input type="c ...

  2. vue+elementUI中单选框el-radio设置默认值和唯一标识某个单选框

    vue+elementUI中单选框el-radio设置默认值 如果后台返回的单选框的值是number:单选框的lable需要设置成 :lable='0';如下: <el-form-item la ...

  3. 单选框radio改变事件详解(用的jquery的radio的change事件)

    单选框radio改变事件详解(用的jquery的radio的change事件) 一.总结 1.用的jquery的radio的change事件:当元素的值发生改变时,会发生 change 事件,radi ...

  4. 纯css美化下拉框、复选框以及单选框样式并用jquery获取到其被选中的val

    具体样式如图所示: 注:获取val值时记得要先引入jquery库奥. 1.下拉框 css部分 #cargo_type_id{ font-size: 13px; border: solid 1px #b ...

  5. js自定义修改复选框单选框样式,清除复选框单选框默认样式

    之前做项目的时候,也遇到过需要按照设计稿把<input type="checkbox">和<input type="radio">的默认 ...

  6. form中 单选框 input[type="radio"] 分组

    在form中有时候需要给单选框分组,这种情况下 可以通过给单选框设置相同的name来进行分组: <html> <head> <title> </title&g ...

  7. WPF中单选框RadioButton

    单选框RadioButton的基本使用: <StackPanel Margin="10"> <Label FontWeight="Bold"& ...

  8. vue中单选框与多选框的实现与美化

    我们在做一些页面时,可能会用到很多的单选框和复选框,但是原生的radio和checkbox前面的原型图标或方框样式不尽人意.于是,决定自己来实现单选框和复选框.我用的是vue,所以就用vue的方式实现 ...

  9. vue.单选和多选,纯css自定义单选框样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. iOS View

    创建: 2018/04/19 完成: 2018/04/20 View的创建  创建  storyboard上操作  与代码连接 ● 目的: 通过代码控制view ● 按住option拖动 View的坐 ...

  2. HDU6069:Counting Divisors(因子数统计|区间筛)

    题意 计算\(\sum_{i=l}^kd(i^k)(d_i代表i的因子数)\) 分析 比赛搞了3个小时都没搞出来,有两个思维上的trick 1.要先遍历素数,再遍历[L,R],而不是枚举每个数,然后对 ...

  3. Codeforces 378C

    DFS连通块,思路就是搜到底,然后一个一个回溯(填上X)上来 #include <iostream> #include <stdio.h> #include <strin ...

  4. hdu 1521 排列组合【指数型生成函数】

    根据套路列出式子:\( \prod_{i=1}^{n}\sum_{j=0}^{c[i]}\frac{x^j}{j!} \),然后暴力展开即可 #include<iostream> #inc ...

  5. .NET Core 跨平台物联网开发:设置委托事件(二)

    系列教程目录 (一) 连接阿里云IOT (二) 设置委托事件 (三) 上报属性 (四)  SDK文档 属性.方法.委托.类 http://pan.whuanle.cn/index.php?dir=up ...

  6. C#解决窗体全屏遮住任务栏

    this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea; 在初始话后添加这个即可实现,...............

  7. Jquery | 基础 | 慕课网 | 基本筛选选择器

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  8. C++构造函数与析构函数的解析

    创建一个对象时,常常需要作某些初始化的工作,例如对数据成员赋初值. 注意,类的数据成员是不能在声明类时初始化的.如果一个类中所有的成员都是公用的,则可以在定义对象时对数据成员进行初始化.如: clas ...

  9. Ubuntu 安装 node

    ubuntu安装node和npm的命令行命令: sudo apt install nodejs-legacy sudo apt install npm 最新版本安装方法 1.安装npm sudo ap ...

  10. Zernike矩之图像重建(附源码)

    源码下载 参考: [1] Teague M R. Image analysis via the general theory of moments[J]. JOSA, 1980, 70(8): 920 ...