How to style a checkbox using CSS

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

I am trying to style a checkbox using the following:

相关标签:
30条回答
  • 2020-11-21 05:04

    Modify checkbox style with plain CSS3, don't required any JS&HTML manipulation.

    .form input[type="checkbox"]:before {
      display: inline-block;
      font: normal normal normal 14px/1 FontAwesome;
      font-size: inherit;
      text-rendering: auto;
      -webkit-font-smoothing: antialiased;
      content: "\f096";
      opacity: 1 !important;
      margin-top: -25px;
      appearance: none;
      background: #fff;
    }
    
    .form input[type="checkbox"]:checked:before {
      content: "\f046";
    }
    
    .form input[type="checkbox"] {
      font-size: 22px;
      appearance: none;
      -webkit-appearance: none;
      -moz-appearance: none;
    }
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
    
    <form class="form">
      <input type="checkbox" />
    </form>

    0 讨论(0)
  • 2020-11-21 05:04

    This is simplest way and you can choose which checkboxes to give this style.

    CSS:

    .check-box input {
      display: none;
    }
    
    .check-box span:before {
      content: ' ';
      width: 20px;
      height: 20px;
      display: inline-block;
      background: url("unchecked.png");
    }
    
    .check-box input:checked + span:before {
      background: url("checked.png");
    }
    

    HTML:

    <label class="check-box">
      <input type="checkbox">
      <span>Check box Text</span>
    </label>
    
    0 讨论(0)
  • 2020-11-21 05:04
    **Custom checkbox with css**  (WebKit browser solution only Chrome, Safari, Mobile browsers)
    
        <input type="checkbox" id="cardAccptance" name="cardAccptance" value="Yes">
        <label for="cardAccptance" class="bold"> Save Card for Future Use</label>
    
        /* The checkbox-cu */
    
        .checkbox-cu {
            display: block;
            position: relative;
            padding-left: 35px;
            margin-bottom: 0;
            cursor: pointer;
            font-size: 16px;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }
    
    
        /* Hide the browser's default checkbox-cu */
    
        .checkbox-cu input {
            position: absolute;
            opacity: 0;
            cursor: pointer;
            height: 0;
            width: 0;
        }
    
    
        /* Create a custom checkbox-cu */
    
        .checkmark {
            position: absolute;
            top: 4px;
            left: 0;
            height: 20px;
            width: 20px;
            background-color: #eee;
            border: 1px solid #999;
            border-radius: 0;
            box-shadow: none;
        }
    
    
        /* On mouse-over, add a grey background color */
    
        .checkbox-cu:hover input~.checkmark {
            background-color: #ccc;
        }
    
    
        /* When the checkbox-cu is checked, add a blue background */
    
        .checkbox-cu input:checked~.checkmark {
            background-color: transparent;
        }
    
    
        /* Create the checkmark/indicator (hidden when not checked) */
    
        .checkmark:after {
            content: "";
            position: absolute;
            display: none;
        }
    
    
        /* Show the checkmark when checked */
    
        .checkbox-cu input:checked~.checkmark:after {
            display: block;
        }
    
    
        /* Style the checkmark/indicator */
    
        .checkbox-cu .checkmark::after {
            left: 7px;
            top: 3px;
            width: 6px;
            height: 9px;
            border: solid #28a745;
            border-width: 0 2px 2px 0;
            -webkit-transform: rotate(45deg);
            -ms-transform: rotate(45deg);
            transform: rotate(45deg);
            z-index: 100;
        }
    
    0 讨论(0)
  • 2020-11-21 05:04

    SCSS / SASS Implementation

    A more modern approach

    For those using SCSS (or easily converted to SASS), the following will be helpful. Effectively, make an element next to the checkbox, which is the one that you will style. When the checkbox is clicked, the CSS restyles the sister element (to your new, checked style). Code is below:

    label.checkbox {
      input[type="checkbox"] {
        visibility: hidden;
        display: block;
        height: 0;
        width: 0;
        position: absolute;
        overflow: hidden;
    
        &:checked + span {
          background: $accent;
        }
      }
    
      span {
        cursor: pointer;
        height: 15px;
        width: 15px;
        border: 1px solid $accent;
        border-radius: 2px;
        display: inline-block;
        transition: all 0.2s $interpol;
      }
    }
    <label class="checkbox">
        <input type="checkbox" />
        <span></span>
        Label text
    </label>

    0 讨论(0)
  • 2020-11-21 05:06

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

    .myCheckbox input {
      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");
    }
    <label for="test">Label for my styled "checkbox"</label>
    <label class="myCheckbox">
      <input type="checkbox" name="test" />
      <span></span>
    </label>

    0 讨论(0)
  • 2020-11-21 05:07

    UPDATE:

    The below answer references the state of things before widespread availability of CSS 3. 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, that writer found 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.

    0 讨论(0)
提交回复
热议问题