美文网首页
一个自定义checkbox

一个自定义checkbox

作者: 流动码文 | 来源:发表于2018-03-27 11:16 被阅读9次
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<style>
  input[type="checkbox"]+label::before {
    content: '\a0';
    display: inline-block;
    vertical-align: center;
    width: .8em;
    height: .8em;
    margin-right: .2em;
    border-radius: .2em;
    background-color: lightcoral;
    text-indent: .15em;
    line-height: .85;
  }

  input[type="checkbox"]:checked+label::before {
    content: '\2713';
    background: lightblue;
  }

  input[type="checkbox"] {
    position: absolute;
    clip: rect(0, 0, 0, 0);
    /* display: none; */
    /* visibility: hidden; */
  }
</style>

<body>
  <input type="checkbox" id="awesome" />
  <label for="awesome">Awesome!</label>
</body>
<script>
</script>

</html>
image.png

一个自定义的checkbox,主要思想是使用伪元素模拟一选择框,把默认的选择框隐藏删除。
这里用了一个一个css样式:

position:absolute;
clip:rect(0,0,0,0);

相关文章

网友评论

      本文标题:一个自定义checkbox

      本文链接:https://www.haomeiwen.com/subject/uvmjcftx.html