How to style a checkbox using CSS

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

I am trying to style a checkbox using the following:

30条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 05:01

    You can achieve quite a cool custom checkbox effect by using the new abilities that come with the :after and :before pseudo classes. The advantage to this, is: You don't need to add anything more to the DOM, just the standard checkbox.

    Note this will only work for compatible browsers. I believe this is related to the fact that some browsers do not allow you to set :after and :before on input elements. Which unfortunately means for the moment only WebKit browsers are supported. Firefox + Internet Explorer will still allow the checkboxes to function, just unstyled, and this will hopefully change in the future (the code does not use vendor prefixes).

    This is a WebKit browser solution only (Chrome, Safari, Mobile browsers)

    See Example Fiddle

    $(function() {
      $('input').change(function() {
        $('div').html(Math.random());
      });
    });
    /* Main Classes */
    .myinput[type="checkbox"]:before {
      position: relative;
      display: block;
      width: 11px;
      height: 11px;
      border: 1px solid #808080;
      content: "";
      background: #FFF;
    }
    
    .myinput[type="checkbox"]:after {
      position: relative;
      display: block;
      left: 2px;
      top: -11px;
      width: 7px;
      height: 7px;
      border-width: 1px;
      border-style: solid;
      border-color: #B3B3B3 #dcffffde #dcffffde #B3B3B3;
      content: "";
      background-image: linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);
      background-repeat: no-repeat;
      background-position: center;
    }
    
    .myinput[type="checkbox"]:checked:after {
      background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAQAAABuW59YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAB2SURBVHjaAGkAlv8A3QDyAP0A/QD+Dam3W+kCAAD8APYAAgTVZaZCGwwA5wr0AvcA+Dh+7UX/x24AqK3Wg/8nt6w4/5q71wAAVP9g/7rTXf9n/+9N+AAAtpJa/zf/S//DhP8H/wAA4gzWj2P4lsf0JP0A/wADAHB0Ngka6UmKAAAAAElFTkSuQmCC'), linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);
    }
    
    .myinput[type="checkbox"]:disabled:after {
      -webkit-filter: opacity(0.4);
    }
    
    .myinput[type="checkbox"]:not(:disabled):checked:hover:after {
      background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAQAAABuW59YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAB2SURBVHjaAGkAlv8A3QDyAP0A/QD+Dam3W+kCAAD8APYAAgTVZaZCGwwA5wr0AvcA+Dh+7UX/x24AqK3Wg/8nt6w4/5q71wAAVP9g/7rTXf9n/+9N+AAAtpJa/zf/S//DhP8H/wAA4gzWj2P4lsf0JP0A/wADAHB0Ngka6UmKAAAAAElFTkSuQmCC'), linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);
    }
    
    .myinput[type="checkbox"]:not(:disabled):hover:after {
      background-image: linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);
      border-color: #85A9BB #92C2DA #92C2DA #85A9BB;
    }
    
    .myinput[type="checkbox"]:not(:disabled):hover:before {
      border-color: #3D7591;
    }
    
    /* Large checkboxes */
    .myinput.large {
      height: 22px;
      width: 22px;
    }
    
    .myinput.large[type="checkbox"]:before {
      width: 20px;
      height: 20px;
    }
    
    .myinput.large[type="checkbox"]:after {
      top: -20px;
      width: 16px;
      height: 16px;
    }
    
    /* Custom checkbox */
    .myinput.large.custom[type="checkbox"]:checked:after {
      background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGHRFWHRBdXRob3IAbWluZWNyYWZ0aW5mby5jb23fZidLAAAAk0lEQVQ4y2P4//8/AyUYwcAD+OzN/oMwshjRBoA0Gr8+DcbIhhBlAEyz+qZZ/7WPryHNAGTNMOxpJvo/w0/uP0kGgGwGaZbrKgfTGnLc/0nyAgiDbEY2BCRGdCDCnA2yGeYVog0Aae5MV4c7Gzk6CRqAbDM2w/EaQEgzXgPQnU2SAcTYjNMAYm3GaQCxNuM0gFwMAPUKd8XyBVDcAAAAAElFTkSuQmCC'), linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);
    }
    
    .myinput.large.custom[type="checkbox"]:not(:disabled):checked:hover:after {
      background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGHRFWHRBdXRob3IAbWluZWNyYWZ0aW5mby5jb23fZidLAAAAk0lEQVQ4y2P4//8/AyUYwcAD+OzN/oMwshjRBoA0Gr8+DcbIhhBlAEyz+qZZ/7WPryHNAGTNMOxpJvo/w0/uP0kGgGwGaZbrKgfTGnLc/0nyAgiDbEY2BCRGdCDCnA2yGeYVog0Aae5MV4c7Gzk6CRqAbDM2w/EaQEgzXgPQnU2SAcTYjNMAYm3GaQCxNuM0gFwMAPUKd8XyBVDcAAAAAElFTkSuQmCC'), linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);
    }
    
    
    
    Normal:
    Small:
    Large:
    Custom icon:

    Bonus Webkit style flipswitch fiddle

    $(function() {
      var f = function() {
        $(this).next().text($(this).is(':checked') ? ':checked' : ':not(:checked)');
      };
      $('input').change(f).trigger('change');
    });
    body {
      font-family: arial;
    }
    
    .flipswitch {
      position: relative;
      background: white;
      width: 120px;
      height: 40px;
      -webkit-appearance: initial;
      border-radius: 3px;
      -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
      outline: none;
      font-size: 14px;
      font-family: Trebuchet, Arial, sans-serif;
      font-weight: bold;
      cursor: pointer;
      border: 1px solid #ffffd;
    }
    
    .flipswitch:after {
      position: absolute;
      top: 5%;
      display: block;
      line-height: 32px;
      width: 45%;
      height: 90%;
      background: #fff;
      box-sizing: border-box;
      text-align: center;
      transition: all 0.3s ease-in 0s;
      color: black;
      border: #888 1px solid;
      border-radius: 3px;
    }
    
    .flipswitch:after {
      left: 2%;
      content: "OFF";
    }
    
    .flipswitch:checked:after {
      left: 53%;
      content: "ON";
    }
    
    
    

    Webkit friendly mobile-style checkbox/flipswitch

     
     

提交回复
热议问题