简体   繁体   中英

Is there a way to style a checkbox or radio button with pure css i.e. using class?

Is there a way to style a checkbox or radio button with pure css ie class="newstyle" without jQuery? I've been searching the web, and I wondering if there is a way to achieve this with only class="" attribute. Here's an example: http://www.filamentgroup.com/examples/customInput/

EDIT: My current way of achieving this:

<div class="styled-checkbox">
 <input type="checkbox" id="checkbox1" value="1" />
 <label for="checkbox1"><span></span>Test</label>
</div>

However, not sure if this is the correct way to achieve this, from a compatibility perspective, in addition, the div needs a pre-set width by default, if auto it will fill the DOM.

You can achieve the desired effect with just one class, but it's wrong to say that you style the checkbox, because it cannot be directly styled via CSS. This is because the component is controlled by the operating system, and not by the browser itself.

The way around is to use a set of normal elements to simulate the checkbox appearance.

In your specific example from the link you provided, the checkbox is completely hidden because it stays behind it's label via z-index , which has a background containing an improved version of checkbox graphic. Combined with the fact that if you use id \\ for attribute pair for the checkbox and for the label, while you click on the label (which displays the image of a checkbox) the checkbox will change it's checked/unchecked state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM