Table of Contents
1、纯css解决方法
2、配合js解决方法
Home Web Front-end HTML Tutorial 表单元素--checkbox样式美化_html/css_WEB-ITnose

表单元素--checkbox样式美化_html/css_WEB-ITnose

Jun 24, 2016 am 11:34 AM

一、背景

 设计狮童鞋总是会设计各种高大上的效果图,比如下面这个土豪金的效果。

 该图中“已阅读并同意相关服务条款”前面的复选框有一个金色的边框,打钩时是一个金色的对勾。接下来说说怎样实现该效果。

二、解决方法

1、纯css解决方法

 在css3 选择器(三)一文中介绍过一个选择器【:checked】选择器。

单选按钮和复选按钮都有选中和未选中状态。要设置这两个按钮默认样式稍微复杂点。该文通过:checked选择器配合其他表情实现自定义样式。

举例:使用:checked选择器模拟实现复选框样式。

<meta charset="utf-8"><style type="text/css">form {  border: 1px solid #ccc;  padding: 20px;  width: 300px;  margin: 30px auto;}.wrapper {  margin-bottom: 10px;}.box {  display: inline-block;  width: 20px;  height: 20px;  margin-right: 10px;  position: relative;  border: 2px solid orange;  vertical-align: middle;}.box input {  opacity: 0;}.box span {  position: absolute;  top: -10px;  right: 3px;  font-size: 30px;  font-weight: bold;  font-family: Arial;  -webkit-transform: rotate(30deg);  transform: rotate(30deg);  color: orange;}input[type="checkbox"] + span {  opacity: 0;}input[type="checkbox"]:checked + span {  opacity: 1;}</style><form action="#">  <div class="wrapper">    <div class="box">      <input type="checkbox" checked="checked" id="username" /><span>√</span>    </div>    <label for="username">我是选中状态</label>  </div>    <div class="wrapper">    <div class="box">      <input type="checkbox"  id="userpwd" /><span>√</span>    </div>    <label for="userpwd">我是未选中状态</label>  </div></form>
Copy after login

模拟实现一个选中和未选中的样式。

没有样式时的效果图如下,

最终添加样式后效果如下。

实际开发中,我尝试使用这种方法。

这种方法有个问题:点选框时无法选中,必须点文字才能选中 。

这在实际应用中肯定是无法忍受的,实际应用中推荐第二种方法。

2、配合js解决方法

用到图片:

原理:label和input外面套一层.custom-checkbox作为父元素相对定位。

input绝对定位于左上角,label也绝对定位于左上角,覆盖input的方块。通过给label设置padding-left和背景图来模拟未选中状态。选中时加一个.right类更改背景图片为选中状态的背景图。

通过js点击事件,label被点击时在选中与未选中状态切换。

<meta charset="utf-8"/><style type="text/css">.custom-checkbox{border:1px solid red;position:relative;height:30px;}.custom-checkbox input{position:absolute;}.custom-checkbox label{padding-left:20px;position:absolute;top:-1px;left:0;background:url(images/bg-unchecked.png) no-repeat top 4px left 3px;}.custom-checkbox label.right{background:url(images/bg-checked.png) no-repeat top 2px left 3px;}</style><body><div class="custom-checkbox"><input type="checkbox" id="test"/><label for="test">已阅读并同意相关服务条款</label></div><script src="http://code.jquery.com/jquery-latest.js"></script><script type="text/javascript"> $('.custom-checkbox label').click(function(){ if($(this).hasClass('right')){ $('.custom-checkbox label').removeClass("right"); }else{ $('.custom-checkbox label').addClass("right"); } });</script></body>
Copy after login

问题:点击频率过快,会双击选中文字,影响用户体验。

解决办法:

<label for="test" onselectstart="return false;" style="-moz-user-select:none;">已阅读并同意相关服务条款</label>
Copy after login

 效果:

三、总结

单选框样式和复选框原理是类似的。上面办法都是一种模拟实现。

正如我在::before和::after伪元素的用法 一文中说过,早在10年我们会使用::before和::after来实现多背景图片,现在css3多背景已成为标准;我们对复选框样式的模拟实现也许会推动它成为标准,这也就是我们为后人做的贡献了,让他们工作更轻松。

还有一点我想说的是推动前端技术发展的,不仅有前端同行们,还有我们可爱的设计师们。如果只有前端,我们会停留在已有技术的可行性上做一些保守的效果,但是有了设计师的“完美主义”,让我们脑洞大开,挑战自我,促进技术更上一层楼。

 

本文作者starof,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:有问题欢迎与我讨论,共同进步。

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

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

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

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

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

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

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

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

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

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

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

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

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

See all articles