设置没有标签标记的复选框的样式

Rai*_*aan 1 html css checkbox label

我做了一个复选框样式,它使用了标签标记。见下文

是否仍可以使整个复选框的CSS没有标签标记?所以我只是有Input标签,但还有CSS。这是复选框的CSS。

.control {
font-size: 18px;
position: relative;
display: block;
margin-bottom: 15px;
padding-left: 30px;
cursor: pointer;
}

.control input {
position: absolute;
z-index: -1;
opacity: 0;
}

.control__indicator {
position: absolute;
top: 2px;
left: 0;
width: 20px;
height: 20px;
background: #e6e6e6;
}

.control--radio .control__indicator {
border-radius: 50%;
}

.control:hover input ~ .control__indicator,
.control input:focus ~ .control__indicator {
background: #ccc;
}

.control input:checked ~ .control__indicator {
background: orange;
}

.control:hover input:not([disabled]):checked ~ .control__indicator,
.control input:checked:focus ~ .control__indicator {
background: #ecb53a;
}

.control input:disabled ~ .control__indicator {
pointer-events: none;
opacity: .6;
background: #e6e6e6;
}

.control__indicator:after {
position: absolute;
display: none;
content: '';
}

.control input:checked ~ .control__indicator:after {
display: block;
}

.control--checkbox .control__indicator:after {
top: 4px;
left: 8px;
width: 3px;
height: 8px;
transform: rotate(45deg);
border: solid black;
border-width: 0 2px 2px 0;
}
Run Code Online (Sandbox Code Playgroud)
<label class="control control--checkbox">
    <input type="checkbox"/>
    <div class="control__indicator"></div>
</label>
Run Code Online (Sandbox Code Playgroud)

我希望你们能帮助我。

Gur*_*har 5

================================================== =================

CSS解决方案(仅适用于Chrome,不适用于FF,IE)

您可以将自定义样式使用CSS的复选框:after:checked

请参见下面的代码或jsfiddle

的HTML

<input type="checkbox"/>
Run Code Online (Sandbox Code Playgroud)

的CSS

    input[type='checkbox']:after{
    line-height: 1.5em;
    content: '';
    display: inline-block;
    width: 18px;
    height: 18px;
    margin-top: -4px;
    margin-left: -4px;
    border: 1px solid rgb(192,192,192);
    border-radius: 0.25em;
    background: rgb(224,224,224);
}

input[type='checkbox']:checked:after {
width: 15px;
    height: 15px;
    border: 3px solid #00ff00;
}
Run Code Online (Sandbox Code Playgroud)

FIDDLE https://jsfiddle.net/guruling/evfr3kk3/

希望这将帮助您应用复选框自定义样式。

================================================== =================

新更新::使用jQuery的跨浏览器解决方案

的HTML

<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
Run Code Online (Sandbox Code Playgroud)

的CSS

.custom-checkbox{
  line-height: 1.5em;
    display: inline-block;
    width: 18px;
    height: 18px;
    margin-top:0px;
    margin-right: -18px;
    border: 1px solid rgb(192,192,192);
    border-radius: 0.25em;
    background: rgb(224,224,224);
    box-sizing:border-box;
}

.custom-checkbox.checked{
    width: 18px;
    height: 18px;
    border: 3px solid #00ff00;
    box-sizing:border-box;
}

input[type="checkbox"]{
  margin: 0px;
  height: 16px;
  width: 19px;
  opacity:0;
}
Run Code Online (Sandbox Code Playgroud)

JS

function setCheckbox(elem){
        if($(elem).is(':checked')){
         $(elem).prev('.custom-checkbox').addClass('checked');
    }else{
       $(elem).prev('.custom-checkbox').removeClass('checked');
    }
}

$(document).ready(function(){
        // apply custom checkbox on page ready
        $('input[type="checkbox"]').before('<span class="custom-checkbox">');
    $('input[type="checkbox"]').each(function(){
          setCheckbox($(this));
        });
});

// on input change, change custom checkbox 
$('body').on('change', 'input[type="checkbox"]', function(){
          setCheckbox($(this));
    });
Run Code Online (Sandbox Code Playgroud)

FIDDLE https://jsfiddle.net/guruling/2rsa7ghn/48/