Html 如何使用css更改输入复选框的背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34388696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:40:34  来源:igfitidea点击:

How to change the background color on a input checkbox with css?

csshtmlcheckboxbackground-color

提问by Ronny Perez

Hello Friends all I'm trying to do is change the background color of a checkbox. I tired many things but nothing works. Can some one help?

你好朋友,我要做的就是更改复选框的背景颜色。我厌倦了很多事情,但没有任何效果。有人可以帮忙吗?

    input[type="checkbox"] {
      background: #3d404e;
      border: #7f83a2 1px solid;
   }

回答by Jinu Kurian

I always use pseudo elements :beforeand :afterfor changing the appearance of checkboxes and radio buttons. it's works like a charm.

我总是用伪元素:before:after改变复选框,单选按钮的外观。它的作用就像一个魅力。

Refer this linkfor more info

有关更多信息,请参阅此链接

CODEPEN

代码笔

Steps

脚步

  1. Hide the default checkbox using css rules like visibility:hiddenor opacity:0or position:absolute;left:-9999pxetc.
  2. Create a fake checkbox using :beforeelement and pass either an empty or a non-breaking space '\00a0';
  3. When the checkbox is in :checkedstate, pass the unicode content: "\2713", which is a checkmark;
  4. Add :focusstyle to make the checkbox accessible.
  5. Done
  1. 使用 css 规则(如visibility:hiddenoropacity:0position:absolute;left:-9999px等)隐藏默认复选框。
  2. 使用:beforeelement创建一个假复选框并传递一个空的或不间断的空格'\00a0'
  3. 当复选框处于:checked状态时,传递 unicode content: "\2713",这是一个复选标记;
  4. 添加:focus样式以使复选框可访问。
  5. 完毕

Here is how I did it.

这是我如何做到的。

.box {
  background: #666666;
  color: #ffffff;
  width: 250px;
  padding: 10px;
  margin: 1em auto;
}
p {
  margin: 1.5em 0;
  padding: 0;
}
input[type="checkbox"] {
  visibility: hidden;
}
label {
  cursor: pointer;
}
input[type="checkbox"] + label:before {
  border: 1px solid #333;
  content: "
<div class="content">
  <div class="box">
    <p>
      <input type="checkbox" id="c1" name="cb">
      <label for="c1">Option 01</label>
    </p>
    <p>
      <input type="checkbox" id="c2" name="cb">
      <label for="c2">Option 02</label>
    </p>
    <p>
      <input type="checkbox" id="c3" name="cb">
      <label for="c3">Option 03</label>
    </p>
  </div>
</div>
a0"; display: inline-block; font: 16px/1em sans-serif; height: 16px; margin: 0 .25em 0 0; padding: 0; vertical-align: top; width: 16px; } input[type="checkbox"]:checked + label:before { background: #fff; color: #333; content: "13"; text-align: center; } input[type="checkbox"]:checked + label:after { font-weight: bold; } input[type="checkbox"]:focus + label::before { outline: rgb(59, 153, 252) auto 5px; }
body{
  font-family: sans-serif;  
}

.container {
    margin-top: 50px;
    margin-left: 20px;
    margin-right: 20px;
}
.checkbox {
    width: 100%;
    margin: 15px auto;
    position: relative;
    display: block;
}

.checkbox input[type="checkbox"] {
    width: auto;
    opacity: 0.00000001;
    position: absolute;
    left: 0;
    margin-left: -20px;
}
.checkbox label {
    position: relative;
}
.checkbox label:before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    margin: 4px;
    width: 22px;
    height: 22px;
    transition: transform 0.28s ease;
    border-radius: 3px;
    border: 2px solid #7bbe72;
}
.checkbox label:after {
  content: '';
    display: block;
    width: 10px;
    height: 5px;
    border-bottom: 2px solid #7bbe72;
    border-left: 2px solid #7bbe72;
    -webkit-transform: rotate(-45deg) scale(0);
    transform: rotate(-45deg) scale(0);
    transition: transform ease 0.25s;
    will-change: transform;
    position: absolute;
    top: 12px;
    left: 10px;
}
.checkbox input[type="checkbox"]:checked ~ label::before {
    color: #7bbe72;
}

.checkbox input[type="checkbox"]:checked ~ label::after {
    -webkit-transform: rotate(-45deg) scale(1);
    transform: rotate(-45deg) scale(1);
}

.checkbox label {
    min-height: 34px;
    display: block;
    padding-left: 40px;
    margin-bottom: 0;
    font-weight: normal;
    cursor: pointer;
    vertical-align: sub;
}
.checkbox label span {
    position: absolute;
    top: 50%;
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%);
}
.checkbox input[type="checkbox"]:focus + label::before {
    outline: 0;
}

Much more stylish using :beforeand :after

更时尚的使用:before:after

<div class="container"> 
  <div class="checkbox">
     <input type="checkbox" id="checkbox" name="" value="">
     <label for="checkbox"><span>Checkbox</span></label>
  </div>

  <div class="checkbox">
     <input type="checkbox" id="checkbox2" name="" value="">
     <label for="checkbox2"><span>Checkbox</span></label>
  </div>
</div>
##代码##