html - 自定义复选框打勾

标签 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>

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

最佳答案

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>

关于html - 自定义复选框打勾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43274765/

相关文章:

javascript - 如何验证一组复选框并返回错误消息javascript

javascript - 隐藏和显示 div 会导致该 div 中的列表滚动到顶部

html - 如何下载单个网页以供离线查看?

javascript - 根据选中的复选框组合重定向到不同的页面

jquery - 如何让 jQuery 在页面加载时点击一个随机的 li 元素?

jquery - 使用 Jquery 和 Ajax 的动画内容

html - 使 Blogger 帖子和页面背景透明

css - 将一个 div 的顶部与另一个 div 的底部合并,高度均为 :auto; in css

jquery - 如果选中复选框,则获取其他输入的内容

javascript - 动态加载包含复选框的 test.php 时,复选框提醒脚本不记得复选框状态