本文介绍了如何使用css更改输入复选框的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好朋友我想要做的就是更改复选框的背景颜色。我厌倦许多事情,但没有任何工作有人可以帮忙吗?

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


解决方案

我总是使用伪元素:之前:之后用于更改复选框和单选按钮的外观。它就像一个魅力。



请参阅此了解更多信息



步骤


  1. 使用css规则隐藏默认复选框,例如 visibility:hidden opacity:0 position:absolute; left:-9999px etc。

  2. 使用创建一个假复选框:before 元素并传递一个空的或不间断的空格'\ 00a0';

  3. 当复选框位于:已选中状态,传递unicode 内容:\ 2713,这是一个复选标记;

  4. 添加:focus 样式以使该复选框可用。

  5. 完成

我是这样做的。



  .box {background:#666666;颜色:#ffffff; width:250px;填充: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;内容:\00a0;显示:inline-block; font:16px / 1em sans-serif; height:16px;保证金:0 .25em 0 0;填充:0; vertical-align:top; width:16px;} input [type =checkbox]:checked + label:before {background:#fff;颜色:#333;内容:\2713; 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;}  
< div类= 内容 > < div class =box> < p为H. < input type =checkboxid =c1name =cb> < label for =c1>选项01< / label> < / p为H. < p为H. < input type =checkboxid =c2name =cb> < label for =c2>选项02< / label> < / p为H. < p为H. < input type =checkboxid =c3name =cb> < label for =c3>选项03< / label> < / p为H. < / div>< / div>

使用:<之后:

body {font-family:sans-serif; } .checkbox {margin:50px auto;位置:相对;显示:块; width:100px;} input [type =checkbox] {width:auto;不透明度:0.00000001;位置:绝对;左:0; margin-left:-20px;}。helper {position:absolute;顶部:-4px; left:-4px;光标:指针;显示:块; font-size:16px;用户选择:无;颜色:#e7e7e7;}。helper:before {content:'';位置:绝对;左:0;顶部:0; margin:4px;宽度:22px;高度:22px;过渡:变换0.28s轻松; border-radius:3px; border:2px solid#7bbe72;}。helper:after {content:'';显示:块;宽度:10px; height:5px; border-bottom:2px solid#7bbe72; border-left:2px solid#7bbe72; -webkit-transform:rotate(-45deg)scale(0); -moz-transform:rotate(-45deg)scale(0); -ms-transform:旋转(-45deg)比例(0); transform:旋转(-45deg)scale(0);位置:绝对; top:12px; left:10px;} input [type =checkbox]:checked~.helper :: before {color:#7bbe72;} input [type =checkbox]:checked~.helper :: after {-webkit-transform:旋转(-45deg)刻度(1); -moz-transform:rotate(-45deg)scale(1); -ms-transform:rotate(-45deg)scale(1); transform:rotate(-45deg)scale(1);}。checkbox label {min-height:24px; padding-left:35px; margin-bottom:0; font-weight:normal;光标:指针; vertical-align:sub;} input [type =checkbox]:focus + label :: before {outline:rgb(59,153,252)auto 5px;}

p>

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;
   }
解决方案

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

Refer this link for more info

Steps

  1. Hide the default checkbox using css rules like visibility:hidden or opacity:0 or position:absolute;left:-9999px etc.
  2. Create a fake checkbox using :before element and pass either an empty or a non-breaking space '\00a0';
  3. When the checkbox is in :checked state, pass the unicode content: "\2713", which is a checkmark;
  4. Add :focus style to make the checkbox accessible.
  5. Done

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: "\00a0";
  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: "\2713";
  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;
}
<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>

Much more stylish using :before and :after

body{
  font-family: sans-serif;  
}

.checkbox {
    margin: 50px auto;
    position: relative;
    display: block;
    width: 100px;
}

input[type="checkbox"] {
    width: auto;
    opacity: 0.00000001;
    position: absolute;
    left: 0;
    margin-left: -20px;
}
.helper {
    position: absolute;
    top: -4px;
    left: -4px;
    cursor: pointer;
    display: block;
    font-size: 16px;
    user-select: none;
    color: #e7e7e7;
}
.helper: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;
}
.helper:after {
  content: '';
    display: block;
    width: 10px;
    height: 5px;
    border-bottom: 2px solid #7bbe72;
    border-left: 2px solid #7bbe72;
    -webkit-transform: rotate(-45deg) scale(0);
    -moz-transform: rotate(-45deg) scale(0);
    -ms-transform: rotate(-45deg) scale(0);
    transform: rotate(-45deg) scale(0);
    position: absolute;
    top: 12px;
    left: 10px;
}
input[type="checkbox"]:checked ~ .helper::before {
    color: #7bbe72;
}

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

.checkbox label {
    min-height: 24px;
    padding-left: 35px;
    margin-bottom: 0;
    font-weight: normal;
    cursor: pointer;
    vertical-align: sub;
}
input[type="checkbox"]:focus + label::before {
    outline: rgb(59, 153, 252) auto 5px;
}
<div class="checkbox">
  <label>
     <input type="checkbox" name="" value="">
     <i class="helper"></i>Checkbox
  </label>
</div>

这篇关于如何使用css更改输入复选框的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 23:55