How to style a checkbox using CSS

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

I am trying to style a checkbox using the following:

30条回答
  •  盖世英雄少女心
    2020-11-21 05:21

    2020 solution - pure CSS checkboxes and radio buttons with no external dependencies

    I have been scrolling and scrolling and tons of these answers simply throw accessibility out the door and violate WCAG in more than one way. I threw in radio buttons since most of the time when you're using custom checkboxes you want custom radio buttons too.

    Fiddles:

    • Checkboxes - pure CSS - free from 3rd party libraries
    • Radio buttons - pure CSS - free from 3rd party libraries
    • Checkboxes* that use FontAwesome but could be swapped with Glyphicons, etc. easily

    Late to the party but somehow this is still difficult in 2019 2020 so I have added my three solutions which are accessible and easy to drop in.

    These are all JavaScript free, external library free*, and accessible...

    If you want to plug-n-play with any of these just copy the style sheet from the fiddles, edit the color codes in the CSS to fit your needs, and be on your way. You can add a custom svg checkmark icon if you want for the checkboxes. I've added lots of comments for those non-CSS'y folks.

    If you have long text or a small container and are encountering text wrapping underneath the checkbox or radio button input then just convert to divs like this.

    Longer explanation: I needed a solution that does not violate WCAG, doesn't rely on JavaScript or external libraries, and that does not break keyboard navigation like tabbing or spacebar to select, that allows focus events, a solution that allows for disabled checkboxes that are both checked and unchecked, and finally a solution where I can customize the look of the checkbox however I want with different background-color's, border-radius, svg backgrounds, etc.

    I used some combination of this answer from @Jan Turoň to come up with my own solution which seems to work quite well. I've done a radio button fiddle that uses a lot of the same code from the checkboxes in order to make this work with radio buttons too.

    I am still learning accessibility so if I missed something please drop a comment and I will try to correct it here is a code example of my checkboxes:

    input[type="checkbox"] {
      position: absolute;
      opacity: 0;
      z-index: -1;
    }
    
    /* Text color for the label */
    input[type="checkbox"]+span {
      cursor: pointer;
      font: 16px sans-serif;
      color: black;
    }
    
    /* Checkbox un-checked style */
    input[type="checkbox"]+span:before {
      content: '';
      border: 1px solid grey;
      border-radius: 3px;
      display: inline-block;
      width: 16px;
      height: 16px;
      margin-right: 0.5em;
      margin-top: 0.5em;
      vertical-align: -2px;
    }
    
    /* Checked checkbox style (in this case the background is green #e7ffba, change this to change the color) */
    input[type="checkbox"]:checked+span:before {
      /* NOTE: Replace the url with a path to an SVG of a checkmark to get a checkmark icon */
      background-image: url('https://cdnjs.cloudflare.com/ajax/libs/ionicons/4.5.6/collection/build/ionicons/svg/ios-checkmark.svg');
      background-repeat: no-repeat;
      background-position: center;
      /* The size of the checkmark icon, you may/may not need this */
      background-size: 25px;
      border-radius: 2px;
      background-color: #e7ffba;
      color: white;
    }
    
    /* Adding a dotted border around the active tabbed-into checkbox */
    input[type="checkbox"]:focus+span:before,
    input[type="checkbox"]:not(:disabled)+span:hover:before {
      /* Visible in the full-color space */
      box-shadow: 0px 0px 0px 2px rgba(0, 150, 255, 1);
    
      /* Visible in Windows high-contrast themes
         box-shadow will be hidden in these modes and
         transparency will not be hidden in high-contrast
         thus box-shadow will not show but the outline will
         providing accessibility */
      outline-color: transparent; /*switch to transparent*/
      outline-width: 2px;
      outline-style: dotted;
      }
    
    
    /* Disabled checkbox styles */
    input[type="checkbox"]:disabled+span {
      cursor: default;
      color: black;
      opacity: 0.5;
    }
    
    /* Styles specific to this fiddle that you do not need */
    body {
      padding: 1em;
    }
    h1 {
      font-size: 18px;
    }

    NOTE: Replace the url for the background-image in CSS with a path to an SVG in your solution or CDN. This one was found from a quick google search for a checkmark icon cdn

    You can easily change the background color, checkbox symbol, border-radius, etc.





提交回复
热议问题