原生 复选框 input[type=“checkbox“] 样式修改

2 篇文章 0 订阅

有的时候在编辑页面的时候会出现需要修改原生复选框样式的需求,那么,这里有一个范本,可以参照修改

input[type="checkbox"] {
  width: 20px;
  height: 20px;
  display: inline-block;
  text-align: center;
  vertical-align: middle;
  line-height: 18px;
  position: relative;
}
input[type="checkbox"]::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  background: #fff;
  width: 100%;
  height: 100%;
  border: 1px solid #d9d9d9;
}
input[type="checkbox"]:checked::before {
  content: "\2713";
  background-color: #fff;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  border: 1px solid #e50232;
  color: #e50232;
  font-size: 20px;
  font-weight: bold;
}

具体自己需要的样式,可根据以上基础进行修改。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 CSS 伪类和伪元素来实现复选框样式,以下是一个简单的示例代码,实现全选、半选、不选三种状态: ```html <!DOCTYPE html> <html> <head> <title>复选框样式示例</title> <style> input[type="checkbox"] { display: none; /* 隐藏原生复选框 */ } label.checkbox { position: relative; display: inline-block; width: 20px; height: 20px; margin-right: 10px; border: 1px solid #ccc; border-radius: 3px; cursor: pointer; } label.checkbox::before { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; background-color: #fff; border-radius: 2px; box-shadow: inset 0 1px 3px rgba(0,0,0,.1); transition: all .2s; } input[type="checkbox"]:checked + label.checkbox::before { background-color: #007bff; } input[type="checkbox"]:checked + label.checkbox::after { content: "\2713"; position: absolute; top: 1px; left: 4px; font-size: 14px; color: #fff; } input[type="checkbox"]:indeterminate + label.checkbox::before { background-color: #ffc107; } input[type="checkbox"]:indeterminate + label.checkbox::after { content: "\2212"; position: absolute; top: 1px; left: 5px; font-size: 14px; color: #fff; } </style> </head> <body> <input type="checkbox" id="checkAll"> <label class="checkbox" for="checkAll"></label> <input type="checkbox" id="check1"> <label class="checkbox" for="check1"></label> <input type="checkbox" id="check2"> <label class="checkbox" for="check2"></label> <script> const checkAll = document.getElementById("checkAll"); const checkboxes = document.querySelectorAll("input[type='checkbox']"); checkAll.addEventListener("click", function() { for (let i = 0; i < checkboxes.length; i++) { checkboxes[i].checked = checkAll.checked; } }); checkboxes.forEach(function(checkbox) { checkbox.addEventListener("click", function() { let checkedCount = document.querySelectorAll("input[type='checkbox']:checked").length; if (checkedCount === 0) { checkAll.checked = false; checkAll.indeterminate = false; } else if (checkedCount === checkboxes.length) { checkAll.checked = true; checkAll.indeterminate = false; } else { checkAll.checked = false; checkAll.indeterminate = true; } }); }); </script> </body> </html> ``` 在这个示例中,我们首先使用 CSS 样式来定义了复选框样式,包括未选中、选中和半选中三种状态。我们使用了 `::before` 和 `::after` 伪元素来实现复选框的外观,并使用了 `:checked` 和 `:indeterminate` 伪类来控制选中和半选中状态下复选框样式。 在 HTML 中,我们定义了三个复选框和一个全选复选框,并使用了 `label` 元素来包裹复选框。我们在 JavaScript 中添加了点击事件监听器,当全选复选框被选中或取消选中时,我们遍历所有的复选框,并将它们的 `checked` 属性设置为全选复选框的状态。当任意一个复选框被选中或取消选中时,我们计算当前被选中的复选框数量,并根据数量来设置全选复选框的状态。这样就可以实现复选框的全选、半选和不选三种样式了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值