我试图使用以下样式的复选框:

<输入类型=“checkbox”样式=“border:2px点数:00f;显示:block;背景:ff0000;”/>

但是没有应用样式。复选框仍然显示其默认样式。我如何给它指定的样式?


当前回答

使用纯CSS,没有花哨的:before和:after,没有转换,您可以关闭默认外观,然后使用内联背景图像对其进行样式化,如下例所示。这适用于Chrome, Firefox, Safari,现在是Edge (Chromium Edge)。

INPUT[type=checkbox]:focus { outline: 1px solid rgba(0, 0, 0, 0.2); } INPUT[type=checkbox] { background-color: #DDD; border-radius: 2px; appearance: none; -webkit-appearance: none; -moz-appearance: none; width: 17px; height: 17px; cursor: pointer; position: relative; top: 5px; } INPUT[type=checkbox]:checked { background-color: #409fd6; background: #409fd6 url("data:image/gif;base64,R0lGODlhCwAKAIABAP////3cnSH5BAEKAAEALAAAAAALAAoAAAIUjH+AC73WHIsw0UCjglraO20PNhYAOw==") 3px 3px no-repeat; } <form> <label><input type="checkbox"> I Agree To Terms &amp; Conditions</label> </form>

其他回答

由于像Edge和Firefox这样的浏览器不支持:before:after on复选框输入标签,这里有一个纯HTML和CSS的替代方案。当然,您应该根据您的要求编辑CSS。

让复选框的HTML像这样:

<div class='custom-checkbox'>
    <input type='checkbox' />
    <label>
        <span></span>
        Checkbox label
    </label>
</div>

为复选框应用此样式可更改颜色标签

<style>
    .custom-checkbox {
        position: relative;
    }
    .custom-checkbox input{
        position: absolute;
        left: 0;
        top: 0;
        height:15px;
        width: 50px;    /* Expand the checkbox so that it covers */
        z-index : 1;    /* the label and span, increase z-index to bring it over */
        opacity: 0;     /* the label and set opacity to 0 to hide it. */
    }
    .custom-checkbox input+label {
        position: relative;
        left: 0;
        top: 0;
        padding-left: 25px;
        color: black;
    }
    .custom-checkbox input+label span {
        position: absolute;  /* a small box to display as checkbox */
        left: 0;
        top: 0;
        height: 15px;
        width: 15px;
        border-radius: 2px;
        border: 1px solid black;
        background-color: white;
    }
    .custom-checkbox input:checked+label { /* change label color when checked */
        color: orange;
    }
    .custom-checkbox input:checked+label span{ /* change span box color when checked */
        background-color: orange;
        border: 1px solid orange;
    }
</style>

开始之前(截至2015年1月)

最初的问题和答案现在已经有5年的历史了。因此,这是一个小小的更新。

首先,当涉及到样式化复选框时,有许多方法。基本宗旨是:

你需要隐藏默认的复选框控件,它是由你的浏览器设置的,并且不能使用CSS以任何有意义的方式覆盖。 在控件隐藏的情况下,您仍然需要能够检测并切换其已选中的状态。 复选框的选中状态需要通过样式化一个新元素来反映。

解决方案(原则上)

上面的任务可以通过许多方法来完成——您经常会听到使用CSS3伪元素是正确的方法。实际上,没有真正的正确或错误的方法,它取决于最适合您将在其中使用它的上下文的方法。也就是说,我有一个更喜欢的。

将复选框包装在标签元素中。这意味着即使它被隐藏,您仍然可以通过单击标签内的任何位置来切换其已检查状态。 隐藏你的复选框。 在复选框后添加一个新元素,您将相应地设置它的样式。它必须出现在复选框之后,以便使用CSS选择它,并根据:checked状态设置样式。CSS不能选择“向后”。

解决方案(代码形式)

label input { visibility: hidden;/* <-- Hide the default checkbox. The rest is to hide and allow tabbing, which display:none prevents */ display: block; height: 0; width: 0; position: absolute; overflow: hidden; } label span {/* <-- Style the artificial checkbox */ height: 10px; width: 10px; border: 1px solid grey; display: inline-block; } [type=checkbox]:checked + span {/* <-- Style its checked state */ background: black; } <label> <input type='checkbox'> <span></span> Checkbox label text </label>

细化(使用图标)

“但是嘿!”我听见你在喊。如果我想在盒子里画一个漂亮的勾号或叉号呢?而且我不想使用背景图片!

这就是CSS3的伪元素可以发挥作用的地方。它们支持content属性,该属性允许您注入表示任一状态的Unicode图标。或者,你也可以使用第三方字体图标源,如font awesome(但要确保你也设置了相关的字体家族,例如FontAwesome)

标签输入{ 显示:没有;/*隐藏默认复选框*/ } /*设置人工复选框*/ 标签跨度{ 高度:10 px; 宽度:10 px; 边框:1px纯灰色; 显示:inline-block; 位置:相对; } /*样式它的checked状态…带有标记图标*/ [type=checkbox]:已检查+ span:before { 内容:“2714 \”; 位置:绝对的; 上图:5 px; 左:0; } <标识> <输入类型=“复选框”> < span > < / span > 复选框标签文本 < / >标签

更新:

下面的答案参考了css3广泛可用之前的情况。在现代浏览器(包括Internet Explorer 9及更高版本)中,使用您喜欢的样式创建复选框替换更简单,而无需使用JavaScript。

以下是一些有用的链接:

使用CSS创建自定义表单复选框 简单的CSS复选框生成器 你可以用复选框黑客做的事情 使用CSS3实现自定义复选框和单选按钮 如何样式的复选框与CSS

It is worth noting that the fundamental issue has not changed. You still can't apply styles (borders, etc.) directly to the checkbox element and have those styles affect the display of the HTML checkbox. What has changed, however, is that it's now possible to hide the actual checkbox and replace it with a styled element of your own, using nothing but CSS. In particular, because CSS now has a widely supported :checked selector, you can make your replacement correctly reflect the checked status of the box.


年长的回答

这是一篇关于设置复选框样式的有用文章。基本上,作者发现不同浏览器的默认复选框差异很大,许多浏览器总是显示默认复选框,无论您如何设置它的样式。所以真的没有简单的方法。

不难想象,可以使用JavaScript将图像覆盖在复选框上,然后单击该图像就会选中真正的复选框。没有JavaScript的用户会看到默认的复选框。

编辑补充:这里有一个很好的脚本,为您做这件事;它隐藏了真正的复选框元素,将其替换为有样式的span,并重定向单击事件。

警告:在撰写本文时,以下内容是正确的,但与此同时,事情有了进展。

AFAIK现代浏览器使用本机操作系统控件显示复选框,所以没有办法对它们进行样式化。

SCSS / SASS实现

更现代的方法

对于使用SCSS(或容易转换为SASS)的人来说,下面的内容将会有所帮助。有效地,在复选框旁边创建一个元素,这就是您要设置样式的元素。当单击复选框时,CSS重新设置姐妹元素的样式(为您选中的新样式)。代码如下:

label.checkbox { input[type="checkbox"] { visibility: hidden; display: block; height: 0; width: 0; position: absolute; overflow: hidden; &:checked + span { background: $accent; } } span { cursor: pointer; height: 15px; width: 15px; border: 1px solid $accent; border-radius: 2px; display: inline-block; transition: all 0.2s $interpol; } } <label class="checkbox"> <input type="checkbox" /> <span></span> Label text </label>