自定义复选框勾选

时间:2017-04-07 09:39:33

标签: html css checkbox

我想知道在点击后如何在我的自定义复选框内进行检查。 我试图隐藏我的自定义下面的常规复选框。用户勾选复选框后,如何获得黑色和简单的勾号?



// Custom Checkbox
.xCheckbox {
  width: 25px;
  margin-left: 40px;
  position: relative;
}


/**
    * The box
    */

.xCheckbox label {
  margin-top: 5px;
  position: absolute;
  cursor: pointer;
  width: 20px;
  height: 20px;
  border-radius: 3px;
  top: 0;
  left: 0;
  background-color: blue;
  border: 1px solid #ddd;
}


/**
    * The tick
    */

.xCheckbox input[type=checkbox]:checked+label {
  transform: rotate(-45deg);
  /* Testing purpose */
}

<div class="xCheckbox">
  <input type="checkbox" value="1" id="xCheckboxInput" name="" />
  <label for="xCheckboxInput"></label>
</div>
&#13;
&#13;
&#13;

http://codepen.io/Maartinshh/pen/dvByge

1 个答案:

答案 0 :(得分:1)

使用label上的伪元素创建勾选并默认隐藏它。选中input后,显示勾号。

此外,要隐藏默认输入框,您可以将其不透明度设置为0:

#customCheckboxInput {
  opacity: 0;
}

codepen

// Custom Checkbox
.customCheckbox {
  width: 25px;
  margin-left: 40px;
  position: relative;
}

#customCheckboxInput {
  opacity: 0;
}


/**
 * The box
 */

.customCheckbox label {
  margin-top: 5px;
  position: absolute;
  cursor: pointer;
  width: 20px;
  height: 20px;
  border-radius: 3px;
  top: 0;
  left: 0;
  background-color: blue;
  border: 1px solid black;
}


/**
 * The tick
 */

.customCheckbox input[type=checkbox]+label::after {
  content: "\2713";
  color: #000;
  text-align: center;
  opacity: 0;
  position: absolute;
  left: 2px;
  transform: rotate(45deg);
  font-weight: bold;
}

.customCheckbox input[type=checkbox]:checked+label {
  transform: rotate(-45deg);
  /* Testing purpose */
  cursor: pointer;
  /* doesnt work like supposed to */
}

.customCheckbox input[type=checkbox]:checked+label::after {
  opacity: 1;
}
<div class="customCheckbox">
  <input type="checkbox" value="1" id="customCheckboxInput" name="" />
  <label for="customCheckboxInput"></label>
</div>

<强>更新

根据要求,要使用多个复选框创建此效果,需要重新处理HTML和CSS。

基本上......将inputlabel包装在容器div中。这解决了position: absolute重叠标签的问题。

我试图在这个例子中简化初始CSS,最值得注意的是完全从伪元素创建自定义复选框,而不是标签的样式。为简单起见,从演示中删除了转换属性。

codepen

.input-container {
  margin-bottom: 10px;
}

/* use :not selector here to support older broswers 
   more info: http://code.stephenmorley.org/html-and-css/styling-checkboxes-and-radio-buttons/
*/

.input-container input[type=checkbox]:not(old) {
  opacity: 0;
  /* match the dimentsions of the custom label */
  width: 20px;
  height: 20px;
  padding: 0;
  margin: 0;
}

/**
 * The box
 */

.input-container label {
  position: relative;
}

.input-container label::after {
  content: "";
  position: absolute;
  /*cover the default input */
  top: -5px;
  left: -20px;
  width: 20px;
  height: 20px;
  border-radius: 3px;
  background-color: blue;
  border: 1px solid black;
  transform: rotate(0);
  pointer-events: none;
  text-align: center;
  color: #FFF; /* easier to see in demo */
}

/**
 * The tick
 */

.input-container input[type=checkbox]:checked+label:after {
  content: "\2713";
}
<div class="customCheckbox">
  <div class="input-container">
    <input type="checkbox" value="1" name="" />
    <label for="customCheckboxInput"></label>
  </div>
  <div class="input-container">
    <input type="checkbox" value="1" name="" />
    <label for="customCheckboxInput"></label>
  </div>
  <div class="input-container">
    <input type="checkbox" value="1" name="" />
    <label for="customCheckboxInput"></label>
  </div>
  <div class="input-container">
    <input type="checkbox" value="1" name="" />
    <label for="customCheckboxInput"></label>
  </div>
</div>