我正在尝试使用以下样式设置复选框:
<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />但是没有应用样式。该复选框仍显示其默认样式。如何给它指定的样式?
我正在尝试使用以下样式设置复选框:
<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />但是没有应用样式。该复选框仍显示其默认样式。如何给它指定的样式?
Answers:
更新:
以下答案引用了CSS 3广泛使用之前的状态。在现代浏览器(包括Internet Explorer 9和更高版本)中,更容易使用首选样式创建复选框替换,而无需使用JavaScript。
以下是一些有用的链接:
值得注意的是,基本问题没有改变。您仍然不能直接将样式(边框等)应用到复选框元素,并使这些样式影响HTML复选框的显示。但是,发生的变化是,现在可以隐藏实际的复选框,并使用您自己的样式化元素替换它,仅使用CSS。特别是,由于CSS现在具有广泛支持的:checked选择器,因此您可以使其替换正确地反映该框的选中状态。
老年答案
这是有关样式复选框的有用文章。基本上,该作者发现,不同浏览器的浏览器差异很大,并且许多浏览器始终显示默认复选框,无论您如何设置其样式。因此,确实没有一个简单的方法。
不难想象有一种解决方法,您可以使用JavaScript在复选框上叠加图像,并在该图像上单击以选中实际的复选框。没有JavaScript的用户将看到默认复选框。
编辑添加:这是一个很好的脚本,可以为您完成;它会隐藏实际的复选框元素,将其替换为样式化的跨度,然后重定向单击事件。
有一种方法可以仅使用CSS来实现。我们可以(ab)使用label元素并为该元素设置样式。需要注意的是,这不适用于Internet Explorer 8及更低版本。
.myCheckbox input {
  position: relative;
  z-index: -9999;
}
.myCheckbox span {
  width: 20px;
  height: 20px;
  display: block;
  background: url("link_to_image");
}
.myCheckbox input:checked + span {
  background: url("link_to_another_image");
}<label for="test">Label for my styled "checkbox"</label>
<label class="myCheckbox">
  <input type="checkbox" name="test" />
  <span></span>
</label>input的永远不会把焦点放在键盘上,因此键盘无法做到这些。
                    通过使用:after和:before伪类随附的新功能,您可以实现非常酷的自定义复选框效果。这样做的好处是:您无需向DOM添加任何其他内容,只需添加标准复选框即可。
请注意,这仅适用于兼容的浏览器。我认为这与某些浏览器不允许您在输入元素上进行设置:after和设置:before有关。不幸的是,这意味着目前仅支持WebKit浏览器。Firefox + Internet Explorer仍将允许复选框起作用,只是不设置样式,并且希望将来会改变(代码不使用供应商前缀)。
这仅是WebKit浏览器解决方案(Chrome,Safari,移动浏览器)
$(function() {
  $('input').change(function() {
    $('div').html(Math.random());
  });
});/* Main Classes */
.myinput[type="checkbox"]:before {
  position: relative;
  display: block;
  width: 11px;
  height: 11px;
  border: 1px solid #808080;
  content: "";
  background: #FFF;
}
.myinput[type="checkbox"]:after {
  position: relative;
  display: block;
  left: 2px;
  top: -11px;
  width: 7px;
  height: 7px;
  border-width: 1px;
  border-style: solid;
  border-color: #B3B3B3 #dcddde #dcddde #B3B3B3;
  content: "";
  background-image: linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);
  background-repeat: no-repeat;
  background-position: center;
}
.myinput[type="checkbox"]:checked:after {
  background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAQAAABuW59YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAB2SURBVHjaAGkAlv8A3QDyAP0A/QD+Dam3W+kCAAD8APYAAgTVZaZCGwwA5wr0AvcA+Dh+7UX/x24AqK3Wg/8nt6w4/5q71wAAVP9g/7rTXf9n/+9N+AAAtpJa/zf/S//DhP8H/wAA4gzWj2P4lsf0JP0A/wADAHB0Ngka6UmKAAAAAElFTkSuQmCC'), linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);
}
.myinput[type="checkbox"]:disabled:after {
  -webkit-filter: opacity(0.4);
}
.myinput[type="checkbox"]:not(:disabled):checked:hover:after {
  background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAQAAABuW59YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAB2SURBVHjaAGkAlv8A3QDyAP0A/QD+Dam3W+kCAAD8APYAAgTVZaZCGwwA5wr0AvcA+Dh+7UX/x24AqK3Wg/8nt6w4/5q71wAAVP9g/7rTXf9n/+9N+AAAtpJa/zf/S//DhP8H/wAA4gzWj2P4lsf0JP0A/wADAHB0Ngka6UmKAAAAAElFTkSuQmCC'), linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);
}
.myinput[type="checkbox"]:not(:disabled):hover:after {
  background-image: linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);
  border-color: #85A9BB #92C2DA #92C2DA #85A9BB;
}
.myinput[type="checkbox"]:not(:disabled):hover:before {
  border-color: #3D7591;
}
/* Large checkboxes */
.myinput.large {
  height: 22px;
  width: 22px;
}
.myinput.large[type="checkbox"]:before {
  width: 20px;
  height: 20px;
}
.myinput.large[type="checkbox"]:after {
  top: -20px;
  width: 16px;
  height: 16px;
}
/* Custom checkbox */
.myinput.large.custom[type="checkbox"]:checked:after {
  background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGHRFWHRBdXRob3IAbWluZWNyYWZ0aW5mby5jb23fZidLAAAAk0lEQVQ4y2P4//8/AyUYwcAD+OzN/oMwshjRBoA0Gr8+DcbIhhBlAEyz+qZZ/7WPryHNAGTNMOxpJvo/w0/uP0kGgGwGaZbrKgfTGnLc/0nyAgiDbEY2BCRGdCDCnA2yGeYVog0Aae5MV4c7Gzk6CRqAbDM2w/EaQEgzXgPQnU2SAcTYjNMAYm3GaQCxNuM0gFwMAPUKd8XyBVDcAAAAAElFTkSuQmCC'), linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);
}
.myinput.large.custom[type="checkbox"]:not(:disabled):checked:hover:after {
  background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGHRFWHRBdXRob3IAbWluZWNyYWZ0aW5mby5jb23fZidLAAAAk0lEQVQ4y2P4//8/AyUYwcAD+OzN/oMwshjRBoA0Gr8+DcbIhhBlAEyz+qZZ/7WPryHNAGTNMOxpJvo/w0/uP0kGgGwGaZbrKgfTGnLc/0nyAgiDbEY2BCRGdCDCnA2yGeYVog0Aae5MV4c7Gzk6CRqAbDM2w/EaQEgzXgPQnU2SAcTYjNMAYm3GaQCxNuM0gFwMAPUKd8XyBVDcAAAAAElFTkSuQmCC'), linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
  <tr>
    <td>Normal:</td>
    <td><input type="checkbox" /></td>
    <td><input type="checkbox" checked="checked" /></td>
    <td><input type="checkbox" disabled="disabled" /></td>
    <td><input type="checkbox" disabled="disabled" checked="checked" /></td>
  </tr>
  <tr>
    <td>Small:</td>
    <td><input type="checkbox" class="myinput" /></td>
    <td><input type="checkbox" checked="checked" class="myinput" /></td>
    <td><input type="checkbox" disabled="disabled" class="myinput" /></td>
    <td><input type="checkbox" disabled="disabled" checked="checked" class="myinput" /></td>
  </tr>
  <tr>
    <td>Large:</td>
    <td><input type="checkbox" class="myinput large" /></td>
    <td><input type="checkbox" checked="checked" class="myinput large" /></td>
    <td><input type="checkbox" disabled="disabled" class="myinput large" /></td>
    <td><input type="checkbox" disabled="disabled" checked="checked" class="myinput large" /></td>
  </tr>
  <tr>
    <td>Custom icon:</td>
    <td><input type="checkbox" class="myinput large custom" /></td>
    <td><input type="checkbox" checked="checked" class="myinput large custom" /></td>
    <td><input type="checkbox" disabled="disabled" class="myinput large custom" /></td>
    <td><input type="checkbox" disabled="disabled" checked="checked" class="myinput large custom" /></td>
  </tr>
</table>$(function() {
  var f = function() {
    $(this).next().text($(this).is(':checked') ? ':checked' : ':not(:checked)');
  };
  $('input').change(f).trigger('change');
});body {
  font-family: arial;
}
.flipswitch {
  position: relative;
  background: white;
  width: 120px;
  height: 40px;
  -webkit-appearance: initial;
  border-radius: 3px;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  outline: none;
  font-size: 14px;
  font-family: Trebuchet, Arial, sans-serif;
  font-weight: bold;
  cursor: pointer;
  border: 1px solid #ddd;
}
.flipswitch:after {
  position: absolute;
  top: 5%;
  display: block;
  line-height: 32px;
  width: 45%;
  height: 90%;
  background: #fff;
  box-sizing: border-box;
  text-align: center;
  transition: all 0.3s ease-in 0s;
  color: black;
  border: #888 1px solid;
  border-radius: 3px;
}
.flipswitch:after {
  left: 2%;
  content: "OFF";
}
.flipswitch:checked:after {
  left: 53%;
  content: "ON";
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<h2>Webkit friendly mobile-style checkbox/flipswitch</h2>
<input type="checkbox" class="flipswitch" />  
<span></span>
<br>
<input type="checkbox" checked="checked" class="flipswitch" />  
<span></span>原始的问题和答案现在已有5年历史了。因此,这只是一个更新。
首先,涉及样式复选框的方法很多。基本宗旨是:
您将需要隐藏浏览器样式的默认复选框控件,并且不能使用CSS以任何有意义的方式覆盖它。
隐藏控件后,您仍然需要能够检测并切换其选中状态
复选框的选中状态需要通过设置新元素的样式来体现
上面的方法可以通过多种方式实现-您经常会听到使用CSS3伪元素是正确的方法。实际上,没有真正的对或错方法,这取决于最适合您将在其中使用上下文的方法。也就是说,我有一个首选方法。
将您的复选框包装在一个label元素中。这意味着即使隐藏了它,也可以在单击标签内的任意位置时切换其选中状态。
隐藏您的复选框
在复选框之后添加一个新元素,您将据此设置样式。它必须出现在复选框之后,以便可以使用CSS进行选择并根据:checked状态设置样式。CSS无法选择“向后”。
但是,嘿!我听到你大喊。如果我想在框中显示一个小的刻度线或十字线怎么办?而且我不想使用背景图片!
嗯,这就是CSS3的伪元素可以发挥作用的地方。它们支持该content属性,该属性使您可以注入表示两种状态的Unicode图标。或者,您可以使用第三方字体图标源,例如font awesome(尽管请确保还设置了相关的font-family,例如为FontAwesome)
label input {
  display: none; /* Hide the default checkbox */
}
/* Style the artificial checkbox */
label span {
  height: 10px;
  width: 10px;
  border: 1px solid grey;
  display: inline-block;
  position: relative;
}
/* Style its checked state...with a ticked icon */
[type=checkbox]:checked + span:before {
  content: '\2714';
  position: absolute;
  top: -5px;
  left: 0;
}<label>
  <input type='checkbox'>
  <span></span>
  Checkbox label text
</label>display: none。
                    visibility: hidden;为opacity: 0 !important;。
                    我会遵循SW4答案的建议–隐藏该复选框并用自定义范围覆盖它,并建议使用以下HTML:
<label>
  <input type="checkbox">
  <span>send newsletter</span>
</label>环绕式标签整齐地允许单击文本,而无需“ for-id”属性链接。然而,
visibility: hidden或隐藏它display: none通过单击或点击它可以工作,但这是使用复选框的一种me脚方式。某些人仍然使用更有效的方法Tab来移动焦点,Space激活焦点,并且使用该方法隐藏会禁用它。如果表格很长,将节省别人的手腕使用tabindex或accesskey属性。并且,如果您观察到系统复选框的行为,则在悬停时会有不错的阴影。样式正确的复选框应遵循此行为。
cobberboy的答案建议使用Font Awesome,它通常比位图好,因为字体是可缩放的矢量。使用上面的HTML,我建议这些CSS规则:
隐藏复选框
input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  z-index: -1;
}我只使用负数,z-index因为我的示例使用了足够大的复选框皮肤来完全覆盖它。我不建议这样做,left: -999px因为它不能在所有布局中重复使用。Bushan wagh的回答提供了一种防弹的方式来隐藏它并说服浏览器使用tabindex,因此它是一个不错的选择。无论如何,两者都只是一个hack。今天的正确方法是appearance: none,参见Joost的回答:
input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
}样式复选框标签
input[type="checkbox"] + span {
  font: 16pt sans-serif;
  color: #000;
}添加复选框皮肤
input[type="checkbox"] + span:before {
  font: 16pt FontAwesome;
  content: '\00f096';
  display: inline-block;
  width: 16pt;
  padding: 2px 0 0 3px;
  margin-right: 0.5em;
}\00f096是Font Awesome的square-o,调整了填充以在焦点上提供均匀的虚线轮廓(请参见下文)。
添加复选框选中的皮肤
input[type="checkbox"]:checked + span:before {
  content: '\00f046';
}\00f046是Font Awesome的check-square-o,它的宽度与相同square-o,这是上面的宽度样式的原因。
添加焦点轮廓
input[type="checkbox"]:focus + span:before {
  outline: 1px dotted #aaa;
}Safari没有提供此功能(请参阅@Jason Sankey的评论),您应该使用它window.navigator来检测浏览器,如果是Safari,则跳过该浏览器。
为禁用复选框设置灰色
input[type="checkbox"]:disabled + span {
  color: #999;
}在未禁用的复选框上设置悬停阴影
input[type="checkbox"]:not(:disabled) + span:hover:before {
  text-shadow: 0 1px 2px #77F;
}尝试将鼠标悬停在复选框上方,然后使用Tab和Shift+ Tab移动和Space切换。
您可以使用label下面的示例,使用一些技巧来对复选框进行样式设置:
.checkbox > input[type=checkbox] {
  visibility: hidden;
}
.checkbox {
  position: relative;
  display: block;
  width: 80px;
  height: 26px;
  margin: 0 auto;
  background: #FFF;
  border: 1px solid #2E2E2E;
  border-radius: 2px;
  -webkit-border-radius: 2px;
  -moz-border-radius: 2px;
}
.checkbox:after {
  position: absolute;
  display: inline;
  right: 10px;
  content: 'no';
  color: #E53935;
  font: 12px/26px Arial, sans-serif;
  font-weight: bold;
  text-transform: capitalize;
  z-index: 0;
}
.checkbox:before {
  position: absolute;
  display: inline;
  left: 10px;
  content: 'yes';
  color: #43A047;
  font: 12px/26px Arial, sans-serif;
  font-weight: bold;
  text-transform: capitalize;
  z-index: 0;
}
.checkbox label {
  position: absolute;
  display: block;
  top: 3px;
  left: 3px;
  width: 34px;
  height: 20px;
  background: #2E2E2E;
  cursor: pointer;
  transition: all 0.5s linear;
  -webkit-transition: all 0.5s linear;
  -moz-transition: all 0.5s linear;
  border-radius: 2px;
  -webkit-border-radius: 2px;
  -moz-border-radius: 2px;
  z-index: 1;
}
.checkbox input[type=checkbox]:checked + label {
  left: 43px;
}<div class="checkbox">
  <input id="checkbox1" type="checkbox" value="1" />
  <label for="checkbox1"></label>
</div>并为上面的代码FIDDLE。请注意,某些CSS在旧版本的浏览器中不起作用,但是我确定那里有一些精美的JavaScript示例!
经过大量搜索和测试,我得到了易于实施且易于定制的解决方案。在此解决方案中:
只需将流动的CSS放在页面顶部,所有复选框的样式都会像这样更改:
input[type=checkbox] {
  transform: scale(1.5);
}
input[type=checkbox] {
  width: 30px;
  height: 30px;
  margin-right: 8px;
  cursor: pointer;
  font-size: 17px;
  visibility: hidden;
}
input[type=checkbox]:after {
  content: " ";
  background-color: #fff;
  display: inline-block;
  margin-left: 10px;
  padding-bottom: 5px;
  color: #00BFF0;
  width: 22px;
  height: 25px;
  visibility: visible;
  border: 1px solid #00BFF0;
  padding-left: 3px;
  border-radius: 5px;
}
input[type=checkbox]:checked:after {
  content: "\2714";
  padding: -5px;
  font-weight: bold;
}您可以避免添加额外的标记。通过设置CSS,除台式机版IE之外,此方法均适用(但适用于Windows Phone和Microsoft Edge的IE)appearance:
input[type="checkbox"] {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  /* Styling checkbox */
  width: 16px;
  height: 16px;
  background-color: red;
}
input[type="checkbox"]:checked {
  background-color: green;
}<input type="checkbox" />我更喜欢使用图标字体(例如FontAwesome),因为使用CSS修改其颜色很容易,并且它们在高像素密度的设备上的缩放比例非常好。因此,这是另一个纯CSS变体,使用与上述类似的技术。
(下面是一个静态图像,因此您可以可视化结果;有关交互式版本,请参见JSFiddle。)

与其他解决方案一样,它使用label元素。相邻项span包含我们的复选框字符。
span.bigcheck-target {
  font-family: FontAwesome; /* Use an icon font for the checkbox */
}
input[type='checkbox'].bigcheck {
  position: relative;
  left: -999em; /* Hide the real checkbox */
}
input[type='checkbox'].bigcheck + span.bigcheck-target:after {
  content: "\f096"; /* In fontawesome, is an open square (fa-square-o) */
}
input[type='checkbox'].bigcheck:checked + span.bigcheck-target:after {
  content: "\f046"; /* fontawesome checked box (fa-check-square-o) */
}
/* ==== Optional - colors and padding to make it look nice === */
body {
  background-color: #2C3E50;
  color: #D35400;
  font-family: sans-serif;
  font-weight: 500;
  font-size: 4em; /* Set this to whatever size you want */
}
span.bigcheck {
  display: block;
  padding: 0.5em;
}<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<span class="bigcheck">
  <label class="bigcheck">
    Cheese
    <input type="checkbox" class="bigcheck" name="cheese" value="yes" />
    <span class="bigcheck-target"></span>
  </label>
</span>这是JSFiddle。
font-family: FontAwesome;为font-family: 'Font Awesome\ 5 Free';,并更新unicode内容。
                    最近,我找到了一个非常有趣的解决方案。
您可以appearance: none;用来关闭复选框的默认样式,然后像此处所述(示例4)在上面写自己的样式。
不幸的是,浏览器支持对 appearance选项来说。根据我的个人测试,我只能使Opera和Chrome正常工作。但这是在获得更好支持或仅想使用Chrome / Opera时保持简单的方法。
appearance,这正是我们需要的。只要记住它“是非标准的并且不在标准轨道上”。
                    使用纯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 & Conditions</label>
</form>input[type="checkbox"] {
  cursor: pointer;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  outline: 0;
  background: lightgray;
  height: 16px;
  width: 16px;
  border: 1px solid white;
}
input[type="checkbox"]:checked {
  background: #2aa1c0;
}
input[type="checkbox"]:hover {
  filter: brightness(90%);
}
input[type="checkbox"]:disabled {
  background: #e6e6e6;
  opacity: 0.6;
  pointer-events: none;
}
input[type="checkbox"]:after {
  content: '';
  position: relative;
  left: 40%;
  top: 20%;
  width: 15%;
  height: 40%;
  border: solid #fff;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
  display: none;
}
input[type="checkbox"]:checked:after {
  display: block;
}
input[type="checkbox"]:disabled:after {
  border-color: #7b7b7b;
}<input type="checkbox"><br>
<input type="checkbox" checked><br>
<input type="checkbox" disabled><br>
<input type="checkbox" disabled checked><br>您可以简单地appearance: none在现代浏览器上使用,以便没有默认样式,并且所有样式都可以正确应用:
input[type=checkbox] {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  display: inline-block;
  width: 2em;
  height: 2em;
  border: 1px solid gray;
  outline: none;
  vertical-align: middle;
}
input[type=checkbox]:checked {
  background-color: blue;
}我一直在滚动浏览,大量的这些答案只是简单地将可访问性扔到了门外,并以多种方式违反了WCAG。自从大多数情况下,当您使用自定义复选框时,我也想添加自定义单选按钮。
小提琴:
晚会晚了,但在某种程度上这在2019 2020年仍然很困难,所以我添加了三个易于使用且易于使用的解决方案。
这些都是JavaScript的自由,外部库免费*,并访问 ...
如果您想即插即用,只需复制小提琴中的样式表,在CSS中编辑颜色代码即可满足您的需要,然后继续使用。如果需要复选框,可以添加自定义svg选中标记图标。我为那些非CSS'y人士添加了很多评论。
如果您有长文本或小容器,并且在复选框或单选按钮输入下面遇到文本换行,则只需将其转换为div 这样。
更长的解释: 
我需要一个不违反WCAG,不依赖JavaScript或外部库且不中断键盘导航(如选择制表键或空格键)的解决方案,允许焦点事件,允许禁用复选框的解决方案。都被选中和未选中,最后是一个解决方案,我可以自定义复选框的外观,但是我想要使用不同background-color的border-radius,svg背景等。
我用这个答案的一些组合 @JanTuroň给出的来提出自己的解决方案,该方法似乎效果很好。我已经完成了一个单选按钮提琴,它使用了复选框中的许多相同代码,以便也可以与单选按钮一起使用。
我仍在学习可访问性,因此,如果我错过了一些事情,请发表评论,我将尝试更正此问题,这是我的复选框的代码示例:
input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  z-index: -1;
}
/* Text color for the label */
input[type="checkbox"]+span {
  cursor: pointer;
  font: 16px sans-serif;
  color: black;
}
/* Checkbox un-checked style */
input[type="checkbox"]+span:before {
  content: '';
  border: 1px solid grey;
  border-radius: 3px;
  display: inline-block;
  width: 16px;
  height: 16px;
  margin-right: 0.5em;
  margin-top: 0.5em;
  vertical-align: -2px;
}
/* Checked checkbox style (in this case the background is red) */
input[type="checkbox"]:checked+span:before {
  /* NOTE: Replace the url with a path to an SVG of a checkmark to get a checkmark icon */
  background-image: url('https://cdnjs.cloudflare.com/ajax/libs/ionicons/4.5.6/collection/build/ionicons/svg/ios-checkmark.svg');
  background-repeat: no-repeat;
  background-position: center;
  /* The size of the checkmark icon, you may/may not need this */
  background-size: 25px;
  border-radius: 2px;
  background-color: #e7ffba;
  color: white;
}
/* Adding a dotted border around the active tabbed-into checkbox */
input[type="checkbox"]:focus+span:before,
input[type="checkbox"]:not(:disabled)+span:hover:before {
  /* Visible in the full-color space */
  box-shadow: 0px 0px 0px 2px rgba(0, 150, 255, 1);
  /* Visible in Windows high-contrast themes
     box-shadow will be hidden in these modes and
     transparency will not be hidden in high-contrast
     thus box-shadow will not show but the outline will
     providing accessibility */
  outline-color: transparent; /*switch to transparent*/
  outline-width: 2px;
  outline-style: dotted;
  }
/* Disabled checkbox styles */
input[type="checkbox"]:disabled+span {
  cursor: default;
  color: black;
  opacity: 0.5;
}
/* Styles specific to this fiddle that you do not need */
body {
  padding: 1em;
}
h1 {
  font-size: 18px;
}<h1>
  NOTE: Replace the url for the background-image in CSS with a path to an SVG in your solution or CDN. This one was found from a quick google search for a checkmark icon cdn
</h1>
<label>
  <input type="checkbox">
  <span>Try using tab and space</span>
</label>
<br>
<label>
  <input type="checkbox" checked disabled>
  <span>Disabled Checked Checkbox</span>
</label>
<br>
<label>
  <input type="checkbox" disabled>
  <span>Disabled Checkbox</span>
</label>
<br>
<label>
  <input type="checkbox">
  <span>Normal Checkbox</span>
</label>
<br>
<label>
  <input type="checkbox">
  <span>Another Normal Checkbox</span>
</label>这是一个简单的CSS解决方案,没有任何jQuery或JavaScript代码。
我正在使用FontAwseome图标,但可以使用任何图像
input[type=checkbox] {
  display: inline-block;
  font-family: FontAwesome;
  font-style: normal;
  font-weight: normal;
  line-height: 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  visibility: hidden;
  font-size: 14px;
}
input[type=checkbox]:before {
  content: @fa-var-square-o;
  visibility: visible;
  /*font-size: 12px;*/
}
input[type=checkbox]:checked:before {
  content: @fa-var-check-square-o;
}label或span元素。就是这样!
                    从我的谷歌搜索来看,这是复选框样式的最简单方法。只需根据您的设计添加:after和:checked:afterCSS。
body{
  background: #DDD;
}
span{
  margin-left: 30px;
}
input[type=checkbox] {
    cursor: pointer;
    font-size: 17px;
    visibility: hidden;
    position: absolute;
    top: 0;
    left: 0;
    transform: scale(1.5);
}
input[type=checkbox]:after {
    content: " ";
    background-color: #fff;
    display: inline-block;
    color: #00BFF0;
    width: 14px;
    height: 19px;
    visibility: visible;
    border: 1px solid #FFF;
    padding: 0 3px;
    margin: 2px 0;
    border-radius: 8px;
    box-shadow: 0 0 15px 0 rgba(0,0,0,0.08), 0 0 2px 0 rgba(0,0,0,0.16);
}
input[type=checkbox]:checked:after {
    content: "\2714";
    display: unset;
    font-weight: bold;
}<input type="checkbox"> <span>Select Text</span>使用纯CSS3修改复选框样式,不需要任何JS&HTML操作。
.form input[type="checkbox"]:before {
  display: inline-block;
  font: normal normal normal 14px/1 FontAwesome;
  font-size: inherit;
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  content: "\f096";
  opacity: 1 !important;
  margin-top: -25px;
  appearance: none;
  background: #fff;
}
.form input[type="checkbox"]:checked:before {
  content: "\f046";
}
.form input[type="checkbox"] {
  font-size: 22px;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
}<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<form class="form">
  <input type="checkbox" />
</form>一个简单而轻巧的模板:
input[type=checkbox] {
  cursor: pointer;
}
input[type=checkbox]:checked:before {
  content: "\2713";
  background: #fffed5;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
  font-size: 20px;
  text-align: center;
  line-height: 8px;
  display: inline-block;
  width: 13px;
  height: 15px;
  color: #00904f;
  border: 1px solid #cdcdcd;
  border-radius: 4px;
  margin: -3px -3px;
  text-indent: 1px;
}
input[type=checkbox]:before {
  content: "\202A";
  background: #ffffff;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
  font-size: 20px;
  text-align: center;
  line-height: 8px;
  display: inline-block;
  width: 13px;
  height: 15px;
  color: #00904f;
  border: 1px solid #cdcdcd;
  border-radius: 4px;
  margin: -3px -3px;
  text-indent: 1px;
}<input type="checkbox" checked="checked">checked1<br>
<input type="checkbox">unchecked2<br>
<input type="checkbox" checked="checked" id="id1">
<label for="id1">checked2+label</label><br>
<label for="id2">unchecked2+label+rtl</label>
<input type="checkbox" id="id2">
<br>input仅在Chrome中受支持,您的代码在每种浏览器中的工作方式都不相同。
                    我认为最简单的方法是将a样式设置label为checkbox不可见。
的HTML
<input type="checkbox" id="first" />
<label for="first"> </label>的CSS
checkbox {
  display: none;
}
checkbox + label {
  /* Style for checkbox normal */
  width: 16px;
  height: 16px;
}
checkbox::checked + label,
label.checked {
  /* Style for checkbox checked */
}的checkbox,即使它是隐藏的,仍然可以访问,并在提交表单时,它的价值将被发送。对于旧的浏览器,你可能需要更改类的label托运使用JavaScript,因为我不认为Internet Explorer中的旧版本理解::checked上checkbox。
::checked是错误的,应该是:checked。(b)checkbox是错的,应该是[type=checkbox]
                    kes!所有这些变通办法使我得出结论:如果要设置样式,HTML复选框会很烂。
作为警告,这不是CSS实现。我只是想分享一下我想出的解决方法,以防其他人发现它有用。
我使用了HTML5 canvas元素。
这样做的好处是您不必使用外部图像,并且可以节省一些带宽。
不利的一面是,如果浏览器由于某种原因无法正确呈现它,那么就不会有后援。尽管这在2017年是否仍然是一个问题尚待商.。
我发现旧代码非常难看,因此决定改写它。
Object.prototype.create = function(args){
    var retobj = Object.create(this);
    retobj.constructor(args || null);
    return retobj;
}
var Checkbox = Object.seal({
    width: 0,
    height: 0,
    state: 0,
    document: null,
    parent: null,
    canvas: null,
    ctx: null,
    /*
     * args:
     * name      default             desc.
     *
     * width     15                  width
     * height    15                  height
     * document  window.document     explicit document reference
     * target    this.document.body  target element to insert checkbox into
     */
    constructor: function(args){
        if(args === null)
            args = {};
        this.width = args.width || 15;
        this.height = args.height || 15;
        this.document = args.document || window.document;
        this.parent = args.target || this.document.body;
        this.canvas = this.document.createElement("canvas");
        this.ctx = this.canvas.getContext('2d');
        this.canvas.width = this.width;
        this.canvas.height = this.height;
        this.canvas.addEventListener("click", this.ev_click(this), false);
        this.parent.appendChild(this.canvas);
        this.draw();
    },
    ev_click: function(self){
        return function(unused){
            self.state = !self.state;
            self.draw();
        }
    },
    draw_rect: function(color, offset){
        this.ctx.fillStyle = color;
        this.ctx.fillRect(offset, offset,
                this.width - offset * 2, this.height - offset * 2);
    },
    draw: function(){
        this.draw_rect("#CCCCCC", 0);
        this.draw_rect("#FFFFFF", 1);
        if(this.is_checked())
            this.draw_rect("#000000", 2);
    },
    is_checked: function(){
        return !!this.state;
    }
});这是一个工作示例。
新版本使用原型和差异继承来创建用于创建复选框的有效系统。要创建一个复选框:
var my_checkbox = Checkbox.create();这将立即将复选框添加到DOM并挂接事件。要查询是否已选中复选框:
my_checkbox.is_checked(); // True if checked, else false还要注意的重要一点是,我摆脱了循环。
我在上一次更新中忽略了的一点是,使用画布比仅制作一个看上去要显示的复选框具有更多优势。如果需要,您还可以创建多状态复选框。
Object.prototype.create = function(args){
    var retobj = Object.create(this);
    retobj.constructor(args || null);
    return retobj;
}
Object.prototype.extend = function(newobj){
    var oldobj = Object.create(this);
    for(prop in newobj)
        oldobj[prop] = newobj[prop];
    return Object.seal(oldobj);
}
var Checkbox = Object.seal({
    width: 0,
    height: 0,
    state: 0,
    document: null,
    parent: null,
    canvas: null,
    ctx: null,
    /*
     * args:
     * name      default             desc.
     *
     * width     15                  width
     * height    15                  height
     * document  window.document     explicit document reference
     * target    this.document.body  target element to insert checkbox into
     */
    constructor: function(args){
        if(args === null)
            args = {};
        this.width = args.width || 15;
        this.height = args.height || 15;
        this.document = args.document || window.document;
        this.parent = args.target || this.document.body;
        this.canvas = this.document.createElement("canvas");
        this.ctx = this.canvas.getContext('2d');
        this.canvas.width = this.width;
        this.canvas.height = this.height;
        this.canvas.addEventListener("click", this.ev_click(this), false);
        this.parent.appendChild(this.canvas);
        this.draw();
    },
    ev_click: function(self){
        return function(unused){
            self.state = !self.state;
            self.draw();
        }
    },
    draw_rect: function(color, offsetx, offsety){
        this.ctx.fillStyle = color;
        this.ctx.fillRect(offsetx, offsety,
                this.width - offsetx * 2, this.height - offsety * 2);
    },
    draw: function(){
        this.draw_rect("#CCCCCC", 0, 0);
        this.draw_rect("#FFFFFF", 1, 1);
        this.draw_state();
    },
    draw_state: function(){
        if(this.is_checked())
            this.draw_rect("#000000", 2, 2);
    },
    is_checked: function(){
        return this.state == 1;
    }
});
var Checkbox3 = Checkbox.extend({
    ev_click: function(self){
        return function(unused){
            self.state = (self.state + 1) % 3;
            self.draw();
        }
    },
    draw_state: function(){
        if(this.is_checked())
            this.draw_rect("#000000", 2, 2);
        if(this.is_partial())
            this.draw_rect("#000000", 2, (this.height - 2) / 2);
    },
    is_partial: function(){
        return this.state == 2;
    }
});我Checkbox对最后一个代码段中使用的内容进行了少许修改,以使其更加通用,从而可以使用具有3种状态的复选框来“扩展”它。这是一个演示。如您所见,它已经具有比内置复选框更多的功能。
在JavaScript和CSS之间进行选择时应考虑的事项。
首先,设置一个画布
var canvas = document.createElement('canvas'),
    ctx = canvas.getContext('2d'),
    checked = 0; // The state of the checkbox
canvas.width = canvas.height = 15; // Set the width and height of the canvas
document.body.appendChild(canvas);
document.body.appendChild(document.createTextNode(' Togglable Option'));接下来,设计一种使画布本身进行更新的方法。
(function loop(){
  // Draws a border
  ctx.fillStyle = '#ccc';
  ctx.fillRect(0,0,15,15);
  ctx.fillStyle = '#fff';
  ctx.fillRect(1, 1, 13, 13);
  // Fills in canvas if checked
  if(checked){
    ctx.fillStyle = '#000';
    ctx.fillRect(2, 2, 11, 11);
  }
  setTimeout(loop, 1000/10); // Refresh 10 times per second
})();最后一部分是使其具有交互性。幸运的是,这非常简单:
canvas.onclick = function(){
  checked = !checked;
}由于IE中奇怪的事件处理模型,因此在IE中可能会遇到问题。
我希望这可以帮助别人; 它绝对适合我的需求。
unchecked (e.g. unchecked -> checked -> "kinda" checked -> unchecked状态可以表示“显式假”,“显式真”,“使用默认值”)。我正在考虑为答案添加示例。
                    这是最简单的方法,您可以选择要使用此样式的复选框。
CSS:
.check-box input {
  display: none;
}
.check-box span:before {
  content: ' ';
  width: 20px;
  height: 20px;
  display: inline-block;
  background: url("unchecked.png");
}
.check-box input:checked + span:before {
  background: url("checked.png");
}HTML:
<label class="check-box">
  <input type="checkbox">
  <span>Check box Text</span>
</label>不需要JavaScript或jQuery。
更改复选框样式的简单方法。
input[type="checkbox"] {
  display: none;
  border: none !important;
  box-shadow: none !important;
}
input[type="checkbox"] + label span {
  background: url(http://imgh.us/uncheck.png);
  width: 49px;
  height: 49px;
  display: inline-block;
  vertical-align: middle;
}
input[type="checkbox"]:checked + label span {
  background: url(http://imgh.us/check_2.png);
  width: 49px;
  height: 49px;
  vertical-align: middle;
}<input type="checkbox" id="option" />
<label for="option"> <span></span> Click me </label>这是仅CSS / HTML的版本,根本不需要jQuery或JavaScript,简单干净的HTML以及真正简单简短的CSS。
这是JSFiddle
这是HTML:
<div id="myContainer">
    <input type="checkbox" name="myCheckbox" id="myCheckbox_01_item" value="red" />
    <label for="myCheckbox_01_item" class="box"></label>
    <label for="myCheckbox_01_item" class="text">I accept the Terms of Use.</label>
</div>这是CSS
#myContainer {
    outline: black dashed 1px;
    width: 200px;
}
#myContainer input[type="checkbox"][name="myCheckbox"] {
    display: none;
}
#myContainer input[type="checkbox"][name="myCheckbox"]:not(:checked) + label.box {
    display: inline-block;
    width: 25px;
    height: 25px;
    border: black solid 1px;
    background: #FFF ;
    margin: 5px 5px;
}
#myContainer input[type="checkbox"][name="myCheckbox"]:checked + label.box {
    display: inline-block;
    width: 25px;
    height: 25px;
    border: black solid 1px;
    background: #F00;
    margin: 5px 5px;
}
#myContainer input[type="checkbox"][name="myCheckbox"] + label + label.text {
    font: normal 12px arial;
    display: inline-block;
    line-height: 27px;
    vertical-align: top;
    margin: 5px 0px;
}这可以被适配为能够具有单独的单选框或复选框,复选框组以及单选按钮组。
此html / css还可让您捕获标签上的点击,因此即使您仅单击标签,该复选框也将被选中和取消选中。
这种类型的复选框/单选按钮适用于任何形式,完全没有问题。也已经使用PHP,ASP.NET(.aspx),JavaServer Faces和ColdFusion进行了测试。
**Custom checkbox with css**  (WebKit browser solution only Chrome, Safari, Mobile browsers)
    <input type="checkbox" id="cardAccptance" name="cardAccptance" value="Yes">
    <label for="cardAccptance" class="bold"> Save Card for Future Use</label>
    /* 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;
    }input[type=checkbox].css-checkbox {
    position: absolute;
    overflow: hidden;
    clip: rect(0 0 0 0);
    height: 1px;
    width: 1px;
    margin: -1px;
    padding: 0;
    border: 0;
}
input[type=checkbox].css-checkbox + label.css-label {
    padding-left: 20px;
    height: 15px;
    display: inline-block;
    line-height: 15px;
    background-repeat: no-repeat;
    background-position: 0 0;
    font-size: 15px;
    vertical-align: middle;
    cursor: pointer;
}
input[type=checkbox].css-checkbox:checked + label.css-label {
    background-position: 0 -15px;
}
.css-label{
    background-image:url(http://csscheckbox.com/checkboxes/dark-check-green.png);
}不,您仍然不能设置复选框的样式,但是我(终于)想出了如何在保留功能的同时设置错觉的样式单击复选框。这意味着即使光标不是完全静止也可以切换它,而不必冒险选择文本或触发拖放操作!
此解决方案也可能适合单选按钮。
以下内容适用于Internet Explorer 9,Firefox 30.0和Chrome 40.0.2214.91,仅是一个基本示例。您仍然可以将其与背景图像和伪元素结合使用。
http://jsfiddle.net/o0xo13yL/1/
label {
    display: inline-block;
    position: relative; /* Needed for checkbox absolute positioning */
    background-color: #eee;
    padding: .5rem;
    border: 1px solid #000;
    border-radius: .375rem;
    font-family: "Courier New";
    font-size: 1rem;
    line-height: 1rem;
}
label > input[type="checkbox"] {
    display: block;
    position: absolute; /* Remove it from the flow */
    width: 100%;
    height: 100%;
    margin: -.5rem; /* Negative the padding of label to cover the "button" */
    cursor: pointer;
    opacity: 0; /* Make it transparent */
    z-index: 666; /* Place it on top of everything else */
}
label > input[type="checkbox"] + span {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 1px solid #000;
    margin-right: .5rem;
}
label > input[type="checkbox"]:checked + span {
    background-color: #666;
}
<label>
    <input type="checkbox" />
    <span> </span>Label text
</label>由于诸如Edge和Firefox之类的浏览器不支持复选框输入标签上的:before:after,因此这是纯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>对于使用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>