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

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

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


当前回答

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>

其他回答

最近我发现了一个很有趣的解决方法。

你可以用appearance: none;关闭复选框的默认样式,然后像这里描述的那样在上面写你自己的样式(例4)。

input[type=checkbox] { width: 23px; height: 23px; -webkit-appearance: none; -moz-appearance: none; appearance: none; margin-right: 10px; background-color: #878787; outline: 0; border: 0; display: inline-block; -webkit-box-shadow: none !important; -moz-box-shadow: none !important; box-shadow: none !important; } input[type=checkbox]:focus { outline: none; border: none !important; -webkit-box-shadow: none !important; -moz-box-shadow: none !important; box-shadow: none !important; } input[type=checkbox]:checked { background-color: green; text-align: center; line-height: 15px; } <input type="checkbox">

不幸的是,浏览器对外观选项的支持很差。从我的个人测试来看,我只有Opera和Chrome可以正常工作。但是当更好的支持到来或者你只想使用Chrome/Opera时,这将是保持简单的方式。

例子JSFiddle

“我能用吗?”链接

自定义复选框与CSS (WebKit浏览器解决方案仅Chrome, Safari,移动浏览器)

<input type="checkbox" id="cardAccptance" name="cardAccptance" value="Yes">
<label for="cardAccptance" class="bold"> Save Card for Future Use</label>

CSS:

    /* The checkbox-cu */
    
    .checkbox-cu {
        display: block;
        position: relative;
        padding-left: 35px;
        margin-bottom: 0;
        cursor: pointer;
        font-size: 16px;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
    }
    
    
    /* Hide the browser's default checkbox-cu */
    
    .checkbox-cu input {
        position: absolute;
        opacity: 0;
        cursor: pointer;
        height: 0;
        width: 0;
    }
    
    
    /* Create a custom checkbox-cu */
    
    .checkmark {
        position: absolute;
        top: 4px;
        left: 0;
        height: 20px;
        width: 20px;
        background-color: #eee;
        border: 1px solid #999;
        border-radius: 0;
        box-shadow: none;
    }
    
    
    /* On mouse-over, add a grey background color */
    
    .checkbox-cu:hover input~.checkmark {
        background-color: #ccc;
    }
    
    
    /* When the checkbox-cu is checked, add a blue background */
    
    .checkbox-cu input:checked~.checkmark {
        background-color: transparent;
    }
    
    
    /* Create the checkmark/indicator (hidden when not checked) */
    
    .checkmark:after {
        content: "";
        position: absolute;
        display: none;
    }
    
    
    /* Show the checkmark when checked */
    
    .checkbox-cu input:checked~.checkmark:after {
        display: block;
    }
    
    
    /* Style the checkmark/indicator */
    
    .checkbox-cu .checkmark::after {
        left: 7px;
        top: 3px;
        width: 6px;
        height: 9px;
        border: solid #28a745;
        border-width: 0 2px 2px 0;
        -webkit-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
        transform: rotate(45deg);
        z-index: 100;
    }

开始之前(截至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 > 复选框标签文本 < / >标签

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

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

这帮助我更改复选框的样式(颜色)

input[type=checkbox] {
  accent-color: red;
}

我们也可以对单选按钮使用相同的方法。