CSS set mobile checkbox and radio style

Source: Internet
Author: User
Tags unsupported

check box ' checkbox ' is a common Web app control that can be seen everywhere, and native check box controls generally look like this:

Depending on the operating system and the browser, sometimes this does not meet the design requirements, which requires a more refined check box style. In the past, only a handful of browsers supported applying styles to such controls, such as getting a design drawing:

Blue.png

In the past, it was impossible to achieve this design effect by simply modifying the style, but now with the powerful CSS3 property appearance you can have unprecedented style control over that type of control:

{  -webkit-appearance: none;}

This sets the value of the property to none remove the original rendering of the check box, become an ordinary element, and then you can apply the style, add the following style:

{  -webkit-appearance: none;   background: #fff url (i/blue.png);   height: 22px;   vertical-align: Middle;   width: 22px;}

By using the state pseudo-class selector :checked , you can set different styles for the checkbox in the selected state to visually differentiate:

{  background-position: -48px 0;}

When you click the checkbox, you can see the change in the check box style, and also, depending on the design picture, you have to add a style that takes focus, disables the status, and so on:

{  background-position: -24px 0;   outline: none;}  {  background-position: -48px 0;}  {  background-position: -72px 0;}  {  background-position: -96px 0;}

Because the picture has been merged into a single, simple modification background-position can be, while the first few selectors priority (weight), so the writing order is very important.

Compatibility

Currently only compatible with the Webkit series browser, although Firefox has implemented an alternative to the -moz-appearance properties, but the original control background color, border style can not be modified, temporarily is not easy to use; This property is temporarily not supported in the IE series, more detailed compatibility View caniuse/ Appearance. So you need to remove the effect of the background image for IE browser:

{  background: #fff url (i/blue.png);   background: none\0;   *background: none;   ...}

In order to be compatible with more mainstream browsers, you need to find another solution, in all major browsers, clicking on the label associated with a check box will result in the same effect as clicking on the element itself, which toggles the check box control's selected state. This behavior of the browser gives us a crucial hook, since we can rely on the label element to control the state of the native check box, then you can not directly manipulate the actual check box elements, and the operation and style are transferred to the associated label element up:

<id= "Example"  type= "checkbox"><  for= "Example"></label>

Make sure that the value of the property of the label element is consistent with the value of the check box input, and that the for id label element is placed after input, so that the CSS can be located in the adjacent sibling selector (adjacent sibling selector) Label element and apply a style to it:

{  background: #fff url (i/blue.png);   content: "";   height: 22px;   Left: 0;   position: absolute;   width: 22px;}

With the styling of the label element to provide interaction, the native CheckBox control is a bit superfluous, although it can be display: none hidden, but the hidden form element is not focusable, so the best way is to use the label element to cover it, so that can support keyboard interaction, At the same time, when the picture fails to load, it can ensure that the native control is available:

{  box-sizing: border-box;   Left: 4px;   margin: 0;   padding: 0;   position: absolute;   top: 3px;}

The picture should be large enough to completely block the Native CheckBox control because it uses absolute positioning, so you need to add a positioning reference:

<!--HTML -<Divclass= "checkbox">  <inputID= "Examplecheckbox"type= "checkbox">  <label for= "Examplecheckbox"></label></Div>/ * CSS */.checkbox {min-height:24px;  padding-left:24px; position:relative;}

The left margin is reserved for better typography, and, as before, with other state styles:

{  background-position: -24px 0;}  {  background-position: -48px 0;}  {  background-position: -72px 0;}  {  background-position: -96px 0;}
Compatibility

As long as browsers that support CSS3 selectors are basically compatible, with most of the interactive features of native controls. IE 8 does not support :checked pseudo-class selectors, modifying pseudo :before -elements to double colons ::before can remove the effect on IE 8:

{}

For the compatibility of pseudo-element generation content, see CSS Generated content for pseudo-elements. Admittedly, the above approach assumes that browsers that support :checked pseudo-class selectors also support double-colon pseudo-element notation, and unsupported browsers do not support it, which is a less-than-good way, which is actually incorrect, causing some of the old browsers to be unusable if you use :not() Selectors are more reasonable, use :not(:checked) to add styles to unchecked controls, :not() and :checked belong to a canonical css3-selectors, compatibility should be consistent CSS3 selectors. But the wording changes a little, :checked and :not(:checked) all need to add the basic style:

input[type= "checkbox"]:checked + label:before,input[type= "checkbox"]:not (: Checked) + Label:before{background:#fff URL (i/blue.png);content: " ";Height:22px; Left:0;position:Absolute;width:22px;}input[type= "checkbox"]:not (: Checked): Focus + label:before,input[type= "checkbox"]:not (: Checked) + Label:hover: Before{background-position:-24px 0;}input[type= "checkbox"]:checked + label:before{background-position:-48px 0;}input[type= "checkbox"][disabled]:not (: Checked) + Label:before{background-position:-72px 0;}input[type= "checkbox"][disabled]:checked + label:before{background-position:-96px 0;}

Looking at a simple example, for browsers that do not support :checked pseudo-class selectors (ie 8, for example), JavaScript can be used to modify the real class attributes to differentiate between different states based on the state of the control, such as adding or removing a clas based on whether it is selected or not checked S

// JQuery function () {  $ (this) [$ (this). Prop (' checked ')? ' AddClass ': ' Removeclass '] (' checked ');}); /*  */Input[type= "checkbox"].checked + Label:before {...}

With the previous basics, it is very simple to make a control like a switch button, or a familiar structure:

 <  div  class  = "checkbox"   >  <  input  id  = "Example"   type  = "checkbox"   >  <  label  for  = "Example"  >  Check</ label  >  </ div  >  

First, the shape of the switch is sketched, a rounded rectangle is placed in the middle of a circular button:

input[type= "checkbox"]:checked + label,input[type= "checkbox"]:not (: Checked) + label{Background-color:#e0e0e0;Border-radius:24px;cursor:Pointer;Display:Inline-block;Height:24px;position:relative;text-indent:-9999px;width:48px;}input[type= "checkbox"]:checked + label:after,input[type= "checkbox"]:not (: Checked) + Label:after{Background-color:#fff;Border-radius:20px;content: " ";Height:20px; Left:2px;position:Absolute;Top:2px;width:20px;}

The selected effect simply modifies the background color of the outer border and the position of the middle button:

{  background-color: #8c8;}  {  left: 26px;}

But this jumping change is too stiff to add a point transition effect that looks smoother:

{  -webkit-transition: background-color 0.3s;     transition: background-color 0.3s;}  {  -webkit-transition: Left 0.3s;     Transition: Left 0.3s;}

Click to see the effect, for the middle button part used CSS3 Transforms to replace left the effect better, faster:

{  -webkit-transition:

Left-webkit-transform 0.3s; -o-transition:-o-transform 0.3s; Transition: left transform 0.3s; } input[type= "checkbox"]:checked + label:after { left:26px; -webkit-transform:translatex (24px);-ms-transfor M:translatex (24PX); -o-transform:translatex (24PX); Transform:translatex (24PX); }

Unsupported CSS3 Transforms browsers can still see the change of background color, without affecting usability, see CSS3 Transforms. Refer to the high performance animations for performance issues. A Quick click on the "control" will cause the selected effect to be unable to toggle the state, so the ability to "control" can be selected is removed:

{  (-prefix-) user-select: none;}

The browser vendor prefix here is replaced as appropriate to see a simple example. Of course, you also need to provide the style of focusing and disabling the state, it does not repeat here. All of the above technologies can be applied to the Radio box (radio).

This article originates from: Http://www.tuicool.com/articles/y67jee

CSS set mobile checkbox and radio style

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.