CSS复选框输入样式

168 css

任何input影响每个输入元素的样式.有没有办法指定样式以仅应用复选框而不对每个复选框元素应用类?

And*_*are 290

使用CSS 2,您可以这样做:

input[type='checkbox'] { ... }
Run Code Online (Sandbox Code Playgroud)

这应该是现在相当广泛的支持..要查看对浏览器的支持,请访问:https://webdesign.tutsplus.com/articles/quick-tip-easy-css3-checkboxes-and-radio-buttons--webdesign- 8953

  • 我相信IE 7和更高版本支持属性选择器,实际上是CSS 2.1.http://www.quirksmode.org/css/contents.html (18认同)
  • @realtebo:注意许多样式(边框,背景等)不能直接应用于HTML复选框. (2认同)

小智 24

我最近发现的用于设置Radio Buttons和Checkboxes样式的东西.之前,我不得不使用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;
}
Run Code Online (Sandbox Code Playgroud)

您可以对复选框执行相同操作,显然更改input[type=radio]input[type=checkbox]并更改border-radius:15px;border-radius:4px;.

希望这对你有用.


Cen*_*MUR 12

我创建自己的无标签解决方案

input[type=checkbox] {
         position: relative;
	       cursor: pointer;
    }
    input[type=checkbox]:before {
         content: "";
         display: block;
         position: absolute;
         width: 16px;
         height: 16px;
         top: 0;
         left: 0;
         border: 2px solid #555555;
         border-radius: 3px;
         background-color: white;
}
    input[type=checkbox]:checked:after {
         content: "";
         display: block;
         width: 5px;
         height: 10px;
         border: solid black;
         border-width: 0 2px 2px 0;
         -webkit-transform: rotate(45deg);
         -ms-transform: rotate(45deg);
         transform: rotate(45deg);
         position: absolute;
         top: 2px;
         left: 6px;
}
Run Code Online (Sandbox Code Playgroud)
<input type="checkbox" name="a">
<input type="checkbox" name="a">
<input type="checkbox" name="a">
<input type="checkbox" name="a">
Run Code Online (Sandbox Code Playgroud)

  • 经过几个小时的搜索和测试,这是唯一对我有用的东西。谢谢! (2认同)
  • 很棒的例子 (2认同)

小智 9

感谢所有对这篇文章做出贡献的人。看看我使用纯 CSS 能够完成的工作。

input[type=checkbox] {
  position: absolute;
  cursor: pointer;
  width: 0px;
  height: 0px;
}

input[type=checkbox]:checked:before {
      content: "";
  display: block;
  position: absolute;
  width: 34px;
  height: 34px;
  border: 4px solid #FFCB9A;
  border-radius: 20px;
  background-color: #445768;  
  transition: all 0.2s linear;
}


input[type=checkbox]:before {
  content: "";
  display: block;
  position: absolute;
  width: 34px;
  height: 34px;
  border: 4px solid #FFCB9A;
  border-radius: 3px;
  background-color: #445768;
}


input[type=checkbox]:after {
  content: "";
  display: block;
  width: 0px;
  height: 0px;
  border: solid #FFCB9A;
  border-width: 0 0px 0px 0;
  -webkit-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  transform: rotate(180deg);
  position: absolute;
  top: 0px;
  left: 50px;
  transition: all 0.2s linear;
}

input[type=checkbox]:checked:after {
  content: "";
  display: block;
  width: 12px;
  height: 21px;
  border: solid #FFCB9A;
  border-width: 0 5px 5px 0;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  position: absolute;
  top: 2px;
  left: 14px;
}
Run Code Online (Sandbox Code Playgroud)
<input type="checkbox">
Run Code Online (Sandbox Code Playgroud)

概括

因此,以下内容控制我们通过添加 h:0 和 w:0 隐藏的实际复选框。

input[type=checkbox] {...}
Run Code Online (Sandbox Code Playgroud)

其余的控制虚拟复选框。

这控制选中时虚拟复选框的背景。

input[type=checkbox]:checked:before {...}
Run Code Online (Sandbox Code Playgroud)

当未选中时,这控制虚拟复选框的背景。

input[type=checkbox]:before {...}
Run Code Online (Sandbox Code Playgroud)

当未选中时,这控制虚拟复选框的前景。

input[type=checkbox]:after {...}
Run Code Online (Sandbox Code Playgroud)

当选中时,这控制虚拟复选框的前景。

input[type=checkbox]:checked:after {...}
Run Code Online (Sandbox Code Playgroud)

我真的希望它能像帮助我一样帮助你:)


Jim*_*ell 8

类也很好,例如:

<style>
  form input .checkbox
  {
    /* your checkbox styling */
  }
</style>

<form>
  <input class="checkbox" type="checkbox" />
</form>
Run Code Online (Sandbox Code Playgroud)