How to style a checkbox using CSS

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

I am trying to style a checkbox using the following:

30条回答
  •  生来不讨喜
    2020-11-21 05:00

    Recently I found a quite interesting solution to the problem.

    You could use appearance: none; to turn off the checkbox's default style and then write your own over it like described here (Example 4).

    Example fiddle

    input[type=checkbox] {
      width: 23px;
      height: 23px;
      -webkit-appearance: none;
      -moz-appearance: none;
      appearance: none;
      margin-right: 10px;
      background-color: #878787;
      outline: 0;
      border: 0;
      display: inline-block;
      -webkit-box-shadow: none !important;
      -moz-box-shadow: none !important;
      box-shadow: none !important;
    }
    
    input[type=checkbox]:focus {
      outline: none;
      border: none !important;
      -webkit-box-shadow: none !important;
      -moz-box-shadow: none !important;
      box-shadow: none !important;
    }
    
    input[type=checkbox]:checked {
      background-color: green;
      text-align: center;
      line-height: 15px;
    }

    Unfortunately browser support is quite bad for the appearance option. From my personal testing I only got Opera and Chrome working correctly. But this would be the way to go to keep it simple when better support comes or you only want to use Chrome/Opera.

    "Can I use?" link

提交回复
热议问题