How to style a checkbox using CSS

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

I am trying to style a checkbox using the following:

30条回答
  •  醉酒成梦
    2020-11-21 05:13

    I think the easiest way to do it is by styling a label and making the checkbox invisible.

    HTML

    
    
    

    CSS

    checkbox {
      display: none;
    }
    
    checkbox + label {
      /* Style for checkbox normal */
      width: 16px;
      height: 16px;
    }
    
    checkbox::checked + label,
    label.checked {
      /* Style for checkbox checked */
    }
    

    The checkbox, even though it is hidden, will still be accessible, and its value will be sent when a form is submitted. For old browsers you might have to change the class of the label to checked using JavaScript because I don't think old versions of Internet Explorer understand ::checked on the checkbox.

提交回复
热议问题