修改radio、checkbox、select默认样式的方法_html/css_WEB-ITnose
现在前端页面效果日益丰富,默认的input组件样式显然已经不能满足需求。趁着这次开发的页面中有这方面的需求,在这里整理一下修改 radio 、 checkbox 、 select 的方法。
首先上效果图:
radio and checkbox
修改radio的默认样式有两种常用的方法
纯CSS
此方法需借助CSS3,关键CSS代码如下
.demo1 input[type='radio'],.demo1 input[type="checkbox"]{ display:none;}.demo1 label:before{ content: ""; display: inline-block; width: 17px; height: 16px; margin-right: 10px; position: absolute; left: 0; bottom: 0; background-color: #3797fc;}.demo1 input[type='radio'] + label:before{ border-radius: 8px;}.demo1 input[type='checkbox'] + label:before{ border-radius: 3px;}.demo1 input[type='radio']:checked+label:before{ content: "\2022"; color: #fff; font-size: 30px; text-align: center; line-height: 19px;}.demo1 input[type='checkbox']:checked+label:before{ content: "\2713"; font-size: 15px; color: #f3f3f3; text-align: center; line-height: 17px;}
-
优点:充分借助了CSS3的优势,无需使用js和图片,仅用纯CSS3就可搞定
-
缺点:兼容性较差,仅支持IE9+
js+图片
-
js代码:
$(function(){ $(".demospan").bind("click",function(){ $(this).addClass("on").siblings().removeClass("on"); }) $(".piaochecked").bind("click",function(){ $(this).hasClass("on_check")?$(this).removeClass("on_check"):$(this).addClass("on_check"); // $(this).toggleClass("on_check"); })})
-
css代码
.demospan{ display: inline-block; width: 24px; height: 18px; /*float: left;*/ padding-top: 3px; cursor: pointer; text-align: center; margin-right: 10px; background-image: url(http://sandbox.runjs.cn/uploads/rs/161/i5pmsg7s/inputradio.gif); background-repeat: no-repeat; background-position: -24px 0;}.demo21{ opacity: 0; cursor: pointer; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; filter:alpha(opacity=0);}.on{ background-position: 0 0;}.piaochecked{ display: inline-block; width: 20px; height: 20px; cursor: pointer; margin-left: 10px; text-align: center; background-image: url(http://sandbox.runjs.cn/uploads/rs/161/i5pmsg7s/checkbox_01.gif); background-repeat: no-repeat; background-position: 0 0;}.on_check{ background-position: 0 -21px;}.cbdemo2{ opacity: 0; cursor: pointer; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(opacity=0)"; filter:alpha(opacity=0);}
-
优点:兼容性高,支持IE6+
-
缺点:使用js+图片较为麻烦
select
/*select*/ .select select{ /*复写Chrome和Firefox里面的边框*/ border:1px solid green; /*清除默认样式*/ appearance:none; -moz-appearance:none; -webkit-appearance:none;/*在选择框的最右侧中间显示小箭头图片*/ background: url("http://ourjs.github.io/static/2015/arrow.png") no-repeat scroll right center transparent; /*为下拉小箭头留出一点位置,避免被文字覆盖*/ padding-right: 14px; } /*清除ie的默认选择框样式清除,隐藏下拉箭头*/select::-ms-expand { display: none; }
该方法关键在于清除默认样式,使用css3的appearance属性,但是兼容性较差,仅支持IE9+。若要兼容低版本浏览器,可以使用Div进行模拟。
Todo
-
兼容更低版本浏览器的select样式修改
最后附上演示链接: 修改radio、checkbox和select默认样式

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.
