A way to customize the checkbox effect for CSS and HTML

Source: Internet
Author: User
The checkbox should be a more commonly used HTML feature, but the browser's own checkbox tends to be not very good-looking, and different browser effects are not the same. Let's share the css+html custom checkbox effect.

The checkbox should be a more commonly used HTML feature, but the browser comes with a checkbox that is often not very good-looking and has different browser effects. The customization of the checkbox is presented for the purpose of beautifying and unifying visual effects. Here is a summary of the implementation method.

Implementation ideas

The main way to implement the pure CSS is to use the label tag's simulation function. The label's for property can be associated with a specific INPUT element, even though the input itself cannot be visible to the user, and with a label corresponding to it, the user can directly interact with the label tag to replace the native input--, which leaves room for our style simulations. In short, it is

Hide native input, the process of style definition is left to label (so why not change the checkbox's style directly?). Because the checkbox is a default component of the browser, the style changes are not as convenient as the label, and many properties do not work for the checkbox, such as background, and the label is basically "trampled" on the same style as p.
On the selection event, due to the existence of the CSS's "neighbor selector (e+f)", we can directly take advantage of the HTML default checkbox, eliminating the trouble of JS analog selection.

Demo

Part of the Demo CSS3 property only writes the WebKit prefix, so it is recommended to use the WebKit kernel browser to view this page

HTML code:

<p class= "Wrap" >    <!--' input ' must have an ID that is required for the label to match elements-    <!--can see the ID of each input and the "for" of the label property corresponds to the same string--><input type= "checkbox" id= "checkbox01"/><label for= "checkbox01" ></label>< Input type= "checkbox" id= "checkbox02"/><label for= "checkbox02" ></label><input type= "checkbox" id= "checkbox03"/><label for= "checkbox03" ></label><input type= "checkbox" id= "checkbox04"/>< Label for= "CHECKBOX04" ></label>

The HTML is built, followed by the corresponding CSS.

. wrap {  width:500px;  Background-color: #EEE;  border:2px solid #DEF;} /* Hide All checkboxes */input[type= ' checkbox '] {  display:none;} /* Simulate the label. Background picture casually pieced together, do not spit groove taste *//*   transition effect is to do a background switch effect, here simply demonstration, actually this transition does not add more natural */label {  display: Inline-block;  width:60px;  height:60px;  position:relative;  Background:url (//www.chitanda.me/images/blank.png);  background-position:0 0px;  -webkit-transition:background 0.5s Linear;} /*  Use the status pseudo-class of the adjacent selector and checkbox ': Checked ' to simulate the default check effect (that is, the effect of clicking on the tick)  *//* If this code comment, no feedback will be given to the user after the click *//* Because the label itself is not selected after the click of the state, the checkbox is hidden, this state can only manually simulate */input[type= ' checkbox ']:checked+label {  background-position : 0-60px;}

The effect of the above code is as follows, and it looks as if it can.

But think about it, seemingly missing something: the text of the hint that corresponds to the option

New people who don't know about CSS may be the first to add text after the label with a P tag or a span tag. But this is not a very elegant way. Personal advice with CSS:: Before and:: After pseudo elements (:: Before and: Before is a thing. However, in order to distinguish between "pseudo-element" and "Pseudo-class", the proposal is written in pseudo-elements:: and Pseudo-class:)
The specific content of the pseudo-elements here not to say, (in fact, I also have their grasp of the only use, for this is not understood, it is not fraught)

/* The pseudo element takes effect very simply, the definition ' content ' is good, the rest of the properties are the same as the ordinary p */

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Related Article

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.