How to style a disabled checkbox?

后端 未结 9 629
旧巷少年郎
旧巷少年郎 2021-01-07 16:32

Do you know how I could style a checkbox when it is disabled?

E.g.:



        
相关标签:
9条回答
  • 2021-01-07 16:47

    This is supported by IE too:

    HTML

    class="disabled"
    

    CSS

    .disabled{
    ...
    }
    
    0 讨论(0)
  • 2021-01-07 16:48
    input[type='checkbox'][disabled][checked] {
    width:0px; height:0px;
    }
    input[type='checkbox'][disabled][checked]:after {
     content:'\e013'; position:absolute; 
     margin-top:-10px;
     opacity: 1 !important;
     margin-left:-5px;
     font-family: 'Glyphicons Halflings';
     font-style: normal;
     font-weight: normal;
    }
    
    0 讨论(0)
  • 2021-01-07 16:51

    You can't style a disabled checkbox directly because it's controlled by the browser / OS.

    However you can be clever and replace the checkbox with a label that simulates a checkbox using pure CSS. You need to have an adjacent label that you can use to style a new "pseudo checkbox". Essentially you're completely redrawing the thing but it gives you complete control over how it looks in any state.

    I've thrown up a basic example so that you can see it in action: http://jsfiddle.net/JohnSReid/pr9Lx5th/3/

    Here's the sample:

    input[type="checkbox"] {
        display: none;
    }
    
    label:before {
        background: linear-gradient(to bottom, #fff 0px, #e6e6e6 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);
        border: 1px solid #035f8f;
        height: 36px;
        width: 36px;
        display: block;
        cursor: pointer;
    }
    input[type="checkbox"] + label:before {
        content: '';
        background: linear-gradient(to bottom, #e6e6e6 0px, #fff 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);
        border-color: #3d9000;
        color: #96be0a;
        font-size: 38px;
        line-height: 35px;
        text-align: center;
    }
    
    input[type="checkbox"]:disabled + label:before {
        border-color: #eee;
        color: #ccc;
        background: linear-gradient(to top, #e6e6e6 0px, #fff 100%) repeat scroll 0 0 rgba(0, 0, 0, 0);
    }
    
    input[type="checkbox"]:checked + label:before {
        content: '✓';
    }
    <div><input id="cb1" type="checkbox" disabled checked /><label for="cb1"></label></div>
    <div><input id="cb2" type="checkbox" disabled /><label for="cb2"></label></div>
    <div><input id="cb3" type="checkbox" checked /><label for="cb3"></label></div>
    <div><input id="cb4" type="checkbox" /><label for="cb4"></label></div>

    Depending on your level of browser compatibility and accessibility, some additional tweaks will need to be made.

    0 讨论(0)
  • 2021-01-07 16:53

    Use the attribute selector in the css

    input[disabled]{
      outline:1px solid red; // or whatever
    }
    

    for checkbox exclusively use

    input[type=checkbox][disabled]{
      outline:1px solid red; // or whatever
    }
    

    $('button').click(function() {
      const i = $('input');
    
      if (i.is('[disabled]'))
        i.attr('disabled', false)
      else
        i.attr('disabled', true);
    })
    input[type=checkbox][disabled] {
      outline: 2px solid red;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="checkbox" value="tasd" disabled />
    <input type="text" value="text" disabled />
    <button>disable/enable</button>

    0 讨论(0)
  • 2021-01-07 16:54

    Use the :disabled CSS3 pseudo-selector

    0 讨论(0)
  • 2021-01-07 16:54

    If you're trying to stop someone from updating the checkbox so it appears disabled then just use JQuery

    $('input[type=checkbox]').click(false);

    You can then style the checkbox.

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