How to style a checkbox using CSS

前端 未结 30 3587
日久生厌
日久生厌 2020-11-21 04:26

I am trying to style a checkbox using the following:

30条回答
  •  面向向阳花
    2020-11-21 05:04

    SCSS / SASS Implementation

    A more modern approach

    For those using SCSS (or easily converted to SASS), the following will be helpful. Effectively, make an element next to the checkbox, which is the one that you will style. When the checkbox is clicked, the CSS restyles the sister element (to your new, checked style). Code is below:

    label.checkbox {
      input[type="checkbox"] {
        visibility: hidden;
        display: block;
        height: 0;
        width: 0;
        position: absolute;
        overflow: hidden;
    
        &:checked + span {
          background: $accent;
        }
      }
    
      span {
        cursor: pointer;
        height: 15px;
        width: 15px;
        border: 1px solid $accent;
        border-radius: 2px;
        display: inline-block;
        transition: all 0.2s $interpol;
      }
    }

提交回复
热议问题