Custom checkbox using only CSS and HTML

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

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

HTML/CSS:

4条回答
  •  一向
    一向 (楼主)
    2020-12-17 15:50

    The problem is that you are using the same pseudo element for the square border and the checkmark. The simple solution would be to continue using the :before pseudo element for the border, and then use an :after pseudo element for the checkmark.

    Updated Example

    You would have to absolutely position the :after pseudo element relative to the parent .checkbox-custom label element.

    Here is the updated code:

    .checkbox-custom {
      display: none;
    }
    .checkbox-custom-label {
      display: inline-block;
      position: relative;
      vertical-align: middle;
      margin: 5px;
      cursor: pointer;
    }
    .checkbox-custom + .checkbox-custom-label:before {
      content: '';
      background: #fff;
      border-radius: 5px;
      border: 2px solid #ffffd;
      display: inline-block;
      vertical-align: middle;
      width: 10px; height: 10px;
      padding: 2px; margin-right: 10px;
    }
    .checkbox-custom:checked + .checkbox-custom-label:after {
      content: "";
      padding: 2px;
      position: absolute;
      width: 1px;
      height: 5px;
      border: solid blue;
      border-width: 0 3px 3px 0;
      transform: rotate(45deg);
      top: 2px; left: 5px;
    }

    Checkboxes

提交回复
热议问题