input radio 选中css样式,input radio和checkbox的样式美化

做项目的时候我们常常需要使用单选按钮input[type=radio]和多选框input[type=checkbox],但是默认的样式肯定与UI设计不一致,而且不够美观,这个时候就需要修改默认的样式,方法其实有蛮多,最先想到的是图片,但是有些时候,图片作为背景图片使用,在手机端适配效果不太理想,所以利用css3的重新实现了一遍,注意,因为使用到伪类属性,故在ie8下无效。

原理利用label标签与对应的input关联,设置label标签的样式,同时input设置透明度为0或者隐藏,使用position(定位)让用户看到的label标签的样式,点击label时会选择到对应的input,使用input的:checked伪类选择器来改变选中label的样式。另外,这里小萌介绍两种方法,一种是使用纯CSS美化按钮,另外一种是使用iconfont搭配css美化按钮,dome链接附在文章底部,需要的小伙伴儿可自行下载。

方法一:使用纯css修改单选框radio

HTML

方法二:使用iconfont搭配css修改单选框radio

  •  选项一

  •  选项二

  •  选项三

另外,此方法需要搭配js来实现效果,也很简单$('.method-two label').on('click',function(){

var radioId = $(this).attr('name');

$('.method-two label').removeClass('icon-choice icon-selected') && $(this).addClass('icon-selected').parent().siblings().children('label').addClass('icon-choice');

$('.method-two input[type="radio"]').removeAttr('checked') && $('#' + radioId).attr('checked', 'checked');

});

使用纯css修改多选框checkbox

  • 选项一

  • 选项二

  • 选项三

这里主要展示html部分的代码,具体的css样式的代码,需要的同学可点击下方的DEMO链接进行查看。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以使用 CSS 美化多选框(checkbox)和单选框(radio)。以下是一个简单的示例: HTML 代码: ```html <label class="checkbox"> <input type="checkbox"> <span class="checkmark"></span> Checkbox 1 </label> <label class="radio"> <input type="radio" name="radio"> <span class="checkmark"></span> Radio 1 </label> <label class="radio"> <input type="radio" name="radio"> <span class="checkmark"></span> Radio 2 </label> ``` CSS 代码: ```css /* 隐藏默认的 checkboxradio */ input[type="checkbox"], input[type="radio"] { display: none; } /* 定义 checkboxradio样式 */ .checkmark { display: inline-block; width: 20px; height: 20px; border: 1px solid #999; border-radius: 5px; cursor: pointer; position: relative; } /* 定义被选中checkboxradio样式 */ .checkmark::after { content: ""; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 10px; height: 10px; background-color: #999; border-radius: 50%; display: none; } input[type="checkbox"]:checked + .checkmark::after { display: block; } input[type="radio"]:checked + .checkmark::after { display: block; } /* 定义 label 的样式 */ label { display: block; margin-bottom: 10px; } /* 定义 checkboxradio 的父元素的样式 */ .checkbox, .radio { display: inline-block; vertical-align: top; margin-right: 20px; } /* 定义 checkboxradio 的文本样式 */ .checkbox span, .radio span { margin-left: 10px; font-size: 14px; } ``` 你可以根据需要调整样式,达到想要的美化效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值