Custom checkbox using only CSS and HTML

后端 未结 4 1835
故里飘歌
故里飘歌 2020-12-17 15:23

I need to create a custom checkbox using only html and CSS. So far I have:

HTML/CSS:

4条回答
  •  -上瘾入骨i
    2020-12-17 15:49

    My previous answer is wrong because it uses the ::before pseudo-element selector on an element that isn't a container. Thanks to @BoltClock for pointing out my error. So, I came up with another solution that uses the Checkbox Hack and the CSS3 sibling selector (~).

    Demo at CodePen

    input[type=checkbox] {
      position: absolute;
      top: -9999px;
      left: -9999px;
    }
    input[type=checkbox] ~ label::before {
      content: '\2713';
      display: inline-block;
      text-align: center;
      color: white;
      line-height: 1em;
      width: 1em;
      height: 1em;
      border: 1px inset silver;
      border-radius: 0.25em;
      margin: 0.25em;
    }
    input[type=checkbox]:checked ~ label::before {
      color: black;
    }
    

    I didn't use StackOverflow's "Run Code-Snippet" functionality because it does something weird when I run it. I think it's because of the checkbox hack.

提交回复
热议问题