How to style a checkbox using CSS

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

I am trying to style a checkbox using the following:

30条回答
  •  清歌不尽
    2020-11-21 05:13

    I prefer to use icon fonts (such as FontAwesome) since it's easy to modify their colours with CSS, and they scale really well on high pixel-density devices. So here's another pure CSS variant, using similar techniques to those above.

    (Below is a static image so you can visualize the result; see the JSFiddle for an interactive version.)

    Checkbox example

    As with other solutions, it uses the label element. An adjacent span holds our checkbox character.

    span.bigcheck-target {
      font-family: FontAwesome; /* Use an icon font for the checkbox */
    }
    
    input[type='checkbox'].bigcheck {
      position: relative;
      left: -999em; /* Hide the real checkbox */
    }
    
    input[type='checkbox'].bigcheck + span.bigcheck-target:after {
      content: "\f096"; /* In fontawesome, is an open square (fa-square-o) */
    }
    
    input[type='checkbox'].bigcheck:checked + span.bigcheck-target:after {
      content: "\f046"; /* fontawesome checked box (fa-check-square-o) */
    }
    
    /* ==== Optional - colors and padding to make it look nice === */
    body {
      background-color: #2C3E50;
      color: #D35400;
      font-family: sans-serif;
      font-weight: 500;
      font-size: 4em; /* Set this to whatever size you want */
    }
    
    span.bigcheck {
      display: block;
      padding: 0.5em;
    }
    
    
    
      
    

    Here's the JSFiddle for it.

提交回复
热议问题