Style a checkbox in firefox — remove check and border

后端 未结 4 1786
说谎
说谎 2020-12-01 16:08

How do I style a checkbox in firefox, and have the checkmark and border disappear?

http://jsfiddle.net/moneylotion/qZvtY/

CSS:



        
相关标签:
4条回答
  • 2020-12-01 16:47

    A cleaner solution IMHO that uses pure css to redraw the elements.

    Codepen

                    input[type="checkbox"] {
                opacity: 0;
                position: absolute;
            }
    
            input[type="checkbox"] ~ label:before {
              content: '';
              display: inline-block;
              cursor: pointer;
              ...
              border: 3px solid #999;
              border-radius: 2px;
              transition: .3s;
            }
    
            input[type="checkbox"]:checked ~ label:before {
              background: #333;
            }

    0 讨论(0)
  • 2020-12-01 17:00

    The accepted answer above is great but this slight tweak to the fiddle from Joeytje50 allows the check-boxes to be tabbed to.

    Using opacity 0 instead of display none means the checkbox is still tabbable and hence accessible by keyboard.

    Position absolute places the input checkbox top left of the drawn box meaning your formatting stays neat.

    input[type="checkbox"] {
        opacity: 0;
        position: absolute;
    }
    input[type="checkbox"] + label {
        text-align:center;
        cursor:pointer;
    }
    input[type="checkbox"]:focus + label {
        background-color: #ffffd;
    }
    input[type="checkbox"] + label div {
        display:inline-block;
        line-height: 16px;
        font-size: 12px;
        height: 16px;
        width: 16px;
        margin:-0px 4px 0 0;
        border: 1px solid black;
        color: transparent;
    }
    input[type="checkbox"]:checked + label div {
        color: black;
    }
    <input type="checkbox" id="c1" name="cc" />
    <label for="c1">
        <div>&#x2714;</div>Check Box 1<br />
    </label>
    <input type="checkbox" id="c12" name="cc" />
    <label for="c12">
        <div>&#x2714;</div>Check Box 2<br />
    </label>

    0 讨论(0)
  • 2020-12-01 17:02

    There's a quite easy way you can do this via <label> tags. Just place a label around the checkbox, and insert a dummy element that will be used for the custom styled checkbox. For example:

    label.checkbox input[type="checkbox"] {display:none;}
    label.checkbox span {
      display:inline-block;
      border:2px solid #BBB;
      border-radius:10px;
      width:25px;
      height:25px;
      background:#C33;
      vertical-align:middle;
      margin:3px;
      position: relative;
      transition:width 0.1s, height 0.1s, margin 0.1s;
    }
    label.checkbox :checked + span {
      background:#6F6;
      width:27px;
      height:27px;
      margin: 2px;
    }
    label.checkbox :checked + span:after {
      content: '\2714';
      font-size: 20px;
      position: absolute;
      top: -2px;
      left: 5px;
      color: #99a1a7;
    }
    <label class="checkbox">
      <input type="checkbox"/>
      <span></span>
      I like cake
    </label>


    EDIT: Note that some choices of colours might render the state of your checkbox invisible for colourblind people. When making this code I didn't think of that, but the above demo might be invisible for R/G colourblind people. When implementing this, please do keep that in mind (pick bright/dark colours for example, or show some difference in shape)


    The styles I used are just arbitrary, and you can change that to anything you want. You can even toggle certain text inside it via the ::before pseudo-element, such as what I've done here.

    I wasn't able to open the image url you provided to use in your question, but I think you'll be able to include whatever image you want by simply modifying this code a little. Just change the current background color to the image URL you want to use.

    Note: This won't work in some older browsers.

    0 讨论(0)
  • 2020-12-01 17:02

    This tutsplus tutorial solved my question.

    input[type="checkbox"] {
      display:none;
    }
     
    input[type="checkbox"] + label span {
      display:inline-block;
      width:19px;
      height:19px;
      margin:-1px 4px 0 0;
      vertical-align:middle;
      background:url(https://cdn.tutsplus.com/webdesign/uploads/legacy/tuts/391_checkboxes/check_radio_sheet.png) left top no-repeat;
      cursor:pointer;
    }
    input[type="checkbox"]:checked + label span {
      background:url(https://cdn.tutsplus.com/webdesign/uploads/legacy/tuts/391_checkboxes/check_radio_sheet.png) -19px top no-repeat;
    }
    <input type="checkbox" id="c1" name="cc" />
    <label for="c1"><span></span>Check Box 1</label>

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