简体   繁体   English

有没有一种方法可以使用纯CSS来设置复选框或单选按钮的样式,即使用类?

[英]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? 有没有一种方法可以使用纯CSS来设置复选框或单选按钮的样式,即没有jQuery的class =“ newstyle”? I've been searching the web, and I wondering if there is a way to achieve this with only class="" attribute. 我一直在网上搜索,我想知道是否有一种方法可以仅使用class =“”属性来实现。 Here's an example: http://www.filamentgroup.com/examples/customInput/ 这是一个示例: 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. 但是,从兼容性的角度来看,不确定这是否是实现此目标的正确方法,此外,默认情况下,div需要预设宽度,如果为auto,它将自动填充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. 您只需要一个类就可以实现所需的效果,但是说出您要设置复选框的样式是错误的,因为不能直接通过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. 在您提供的链接的特定示例中,该复选框完全隐藏,因为它通过z-index保留在标签的后面,该标签的背景包含改进的复选框图形版本。 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. 有,如果你使用的事实相结合id \\ for属性对为复选框和标签,同时单击标签(它显示一个复选框的图像)的复选框将改变它的选中/取消选中状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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