2025-02-12 05:00:11

CSS复选框输入样式

输入的任何样式都会影响每个输入元素。是否有一种方法可以指定样式只应用于复选框,而不对每个复选框元素应用类?


当前回答

input[type="checkbox"] {
 /* your style */
}

但这只适用于IE7及以下浏览器,对于那些你必须使用类的浏览器。

其他回答

一些我最近发现的样式单选按钮和复选框。在此之前,我必须使用jQuery和其他工具。但这是愚蠢的简单。

input[type=radio] {
    padding-left:5px;
    padding-right:5px;
    border-radius:15px;

    -webkit-appearance:button;

    border: double 2px #00F;

    background-color:#0b0095;
    color:#FFF;
    white-space: nowrap;
    overflow:hidden;

    width:15px;
    height:15px;
}

input[type=radio]:checked {
    background-color:#000;
    border-left-color:#06F;
    border-right-color:#06F;
}

input[type=radio]:hover {
    box-shadow:0px 0px 10px #1300ff;
}

你可以对复选框做同样的事情,显然改变输入[type=radio]为输入[type=checkbox],并改变border-radius:15px;border - radius: 4 px;。

希望这对你有所帮助。

input[type="checkbox"] {
 /* your style */
}

但这只适用于IE7及以下浏览器,对于那些你必须使用类的浏览器。

你可以使用这个选择器将你的样式应用到复选框输入:

input[type='checkbox']{}

但不幸的是,它不支持自定义样式,所以你可以使用“after”和“before”来覆盖原始输入: 参见:演示

HTML

 <input
    type="checkbox"
    style="background-color: #d81b60; color: #fff"
    name="checkboxGreen"
  />

CSS

input[type='checkbox'] {
      position: relative;
      cursor: pointer;
      margin: 20px;
 }
 input[type='checkbox']:before {
      content: '';
      display: block;
      position: absolute;
      width: 20px;
      height: 20px;
      top: 50%;
      left: 50%;
      background-color: inherit;
      outline: 1px rgba(255, 255, 255, 0.2) solid;
      transform: translate(-50%, -50%);
 }
 input[type='checkbox']:checked:before {     
      content: '';
      display: block;
      position: absolute;
      width: 20px;
      height: 20px;
      top: 50%;
      left: 50%;
      background-color: inherit;
      outline: 1px rgba(255, 255, 255, 0.2) solid;
      transform: translate(-50%, -50%);
      box-shadow: 0px 0px 4px 1px #fff;
 }
 input[type='checkbox']:checked:after {
      content: '';
      display: block;
      width: 6px;
      height: 10px;
      border: solid;
      border-width: 0 2px 2px 0;
      border-color: inherit;
      position: absolute;
      top: 35%;
      left: 50%;
      transform: translate(-50%, -50%) rotate(45deg);
 }

结果:

虽然CSS确实为您提供了一种方法来执行特定于复选框类型或其他类型的样式,但不支持这种样式的浏览器将会出现问题。

我认为在这种情况下,您唯一的选择是将类应用到复选框中。

只需将class="checkbox"添加到您的复选框中。

然后在css代码中创建该样式。

你可以这样做:

main.css

input[type="checkbox"] { /* css code here */ }

ie.css

.checkbox{ /* css code here for ie */ }

然后使用IE特定的css包括:

<!--[if lt IE 7]>
   <link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->

您仍然需要添加类,以便它在IE中工作,并且它不能在其他不支持IE的非IE浏览器中工作。但是它会使你的网站具有前瞻性的css代码,随着IE得到支持,你将能够从复选框中删除IE特定的css代码和css类。

Trident为复选框和单选按钮控件提供了::-ms-check伪元素。例如:

<input type="checkbox">
<input type="radio">

::-ms-check {
    color: red;
    background: black;
    padding: 1em;
}

Windows 8操作系统的IE10界面显示如下: