How to style a checkbox using CSS?

我正在尝试使用以下方式设置复选框的样式:

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

UPDATE: The below answer references the state of things before widespread availability of CSS3. In modern browsers (including Internet Explorer 9 and later) it is more straightforward to create checkbox replacements with your preferred styling, without using javascript.

Here are some useful links:

  • Creating Custom Form Checkboxes with Just CSS
  • Easy CSS Checkbox Generator
  • Stuff You Can Do With The Checkbox Hack
  • Implementing Custom Checkboxes and Radio Buttons with CSS3
  • How to Style a Checkbox With CSS
  • It is worth noting that the fundamental issue 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's now possible to hide the actual checkbox and replace it with a styled element of your own, using nothing but CSS. In particular, because CSS now has a widely supported :checked selector, you can make your replacement correctly reflect the checked status of the box.


    OLDER ANSWER

    Here's a useful article about styling checkboxes. Basically what that writer found was that it varies tremendously from browser to browser, and that many browsers always display the default checkbox no matter how you style it. So there really isn't an easy way.

    It's not hard to imagine a workaround where you would use javascript to overlay an image on the checkbox and have clicks on that image cause the real checkbox to be checked. Users without javascript would see the default checkbox.

    Edited to add: here's a nice script that does this for you; it hides the real checkbox element, replaces it with a styled span, and redirects the click events.


    There is a way to do this using just CSS. We can (ab)use the label element and style that instead. The caveat is that this will not work for IE8 and lower versions.

    CSS:

    .myCheckbox input {
        // display: none;
        // Better than display: none for accessibility reasons
        position: relative;
        z-index: -9999;
    }
    
    .myCheckbox span {
        width: 20px;
        height: 20px;
        display: block;
        background: url("link_to_image");
    }
    
    .myCheckbox input:checked + span {
        background: url("link_to_another_image");
    }
    

    HTML:

    <label for="test">Label for my styled "checkbox"</label>
    <label class="myCheckbox">
        <input type="checkbox" name="test"/>
        <span></span>
    </label>
    

    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. FF + IE 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 #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>
    链接地址: http://www.djcxy.com/p/41450.html

    上一篇: 如何在Rails表单的同一行保留复选框和标签?

    下一篇: 如何使用CSS设计复选框?