search

Home  >  Q&A  >  body text

CSS Tips: How to Style Checkboxes Using CSS

I'm trying to style a checkbox using the following:


<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />


But the style is not applied. The checkbox still displays its default style. How to give it a specific style?

P粉986937457P粉986937457418 days ago761

reply all(2)I'll reply

  • P粉973899567

    P粉9738995672023-10-09 15:58:59

    By using the new functionality that comes with the :after and :before pseudo-classes, you can achieve very cool custom checkbox effects. The advantage of this is that you don't need to add anything else to the DOM, just the standard checkbox.

    Please note that this only works on compatible browsers. I believe this has to do with some browsers not allowing you to set :after and :before on input elements. Unfortunately, this means that currently only WebKit browsers are supported. Firefox Internet Explorer still allows checkboxes to function, just without styling, which is expected to change in the future (the code does not use the vendor prefix).

    This is just a WebKit browser solution (Chrome, Safari, mobile browsers)

    View 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 #dcddde #dcddde #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%);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <table style="width:100%">
      <tr>
        <td>Normal:</td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" disabled="disabled" /></td>
        <td><input type="checkbox" disabled="disabled" checked="checked" /></td>
      </tr>
      <tr>
        <td>Small:</td>
        <td><input type="checkbox" class="myinput" /></td>
        <td><input type="checkbox" checked="checked" class="myinput" /></td>
        <td><input type="checkbox" disabled="disabled" class="myinput" /></td>
        <td><input type="checkbox" disabled="disabled" checked="checked" class="myinput" /></td>
      </tr>
      <tr>
        <td>Large:</td>
        <td><input type="checkbox" class="myinput large" /></td>
        <td><input type="checkbox" checked="checked" class="myinput large" /></td>
        <td><input type="checkbox" disabled="disabled" class="myinput large" /></td>
        <td><input type="checkbox" disabled="disabled" checked="checked" class="myinput large" /></td>
      </tr>
      <tr>
        <td>Custom icon:</td>
        <td><input type="checkbox" class="myinput large custom" /></td>
        <td><input type="checkbox" checked="checked" class="myinput large custom" /></td>
        <td><input type="checkbox" disabled="disabled" class="myinput large custom" /></td>
        <td><input type="checkbox" disabled="disabled" checked="checked" class="myinput large custom" /></td>
      </tr>
    </table>

    Extra Webkit style flipped 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 #ddd;
    }
    
    .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";
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    
    <h2>Webkit friendly mobile-style checkbox/flipswitch</h2>
    <input type="checkbox" class="flipswitch" /> &nbsp;
    <span></span>
    <br>
    <input type="checkbox" checked="checked" class="flipswitch" /> &nbsp;
    <span></span>

    reply
    0
  • P粉851401475

    P粉8514014752023-10-09 00:55:47

    renew:

    The answers below refer to the situation before CSS 3 was widely used. In modern browsers, including Internet Explorer 9 and later, it's easier to create checkbox replacements with your favorite styles, without using JavaScript.

    Here are some useful links:

    It is worth noting that the fundamental problem has not changed. You still can't apply styles (borders, etc.) directly to the checkbox element and have those styles affect the display of the HTML checkbox. What has changed, however, is that it is now possible to hide the actual checkbox and replace it with your own style element, using just CSS. In particular, since CSS now has widely supported :checked selectors, you can make replacements correctly reflect the checked state of the box.


    Old Answer

    This is a useful article on styling checkboxes. Basically, this author found that it varies hugely between browsers, and no matter how you style it, many browsers always show the default checkbox. So there really isn't an easy way.

    It's not hard to imagine a workaround where you could use JavaScript to overlay an image on the checkbox, and then clicking on the image causes the real checkbox to be checked. Users without JavaScript will see the default checkbox.

    Edited to add: Here is a nice script that does this for you ; it hides the real checkbox element, replaces it with a styled scope, and redirects the click event.

    reply
    0
  • Cancelreply