Using method 1 to create a clickable label, hiding the checkbox with CSS and toggling it and the label with jQuery, I got this:
$('label').click(function(e) {
$(this).toggleClass('active');
var cbx = $('input', this);
cbx.prop('checked', !cbx.prop("checked"));
console.log($('input', this).prop('checked'));
return false;
})
input[type="checkbox"] {visibility: hidden;position: absolute}
label {background-color: pink;border: 1px solid lightblue;padding: 20px}
label.active {background-color: lightgreen}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input type="checkbox" checked />Check Button</label>
[Check the console for the checkbox status]
My question is: is it possible to do the same only using CSS?
I realize I'm using <label>
not <button>
and I assume there are better ways to achieve this with jQuery. If you consider replying with one, please do it using a checkbox (or at least explain why you must not use it).
You can do this:
Or try this:
HTML
<input type="checkbox" id="checkbox-1-1" class="regular-checkbox" /><label for="checkbox-1-1"></label>
CSS
.regular-checkbox {
display: none;
}
.regular-checkbox + label {
background-color: #fafafa;
border: 1px solid #cacece;
box-shadow: 0 1px 2px rgba(0,0,0,0.05), inset 0px -15px 10px -12px rgba(0,0,0,0.05);
padding: 9px;
border-radius: 3px;
display: inline-block;
position: relative;
}
.regular-checkbox + label:active, .regular-checkbox:checked + label:active {
box-shadow: 0 1px 2px rgba(0,0,0,0.05), inset 0px 1px 3px rgba(0,0,0,0.1);
}
.regular-checkbox:checked + label {
background-color: #e9ecee;
border: 1px solid #adb8c0;
box-shadow: 0 1px 2px rgba(0,0,0,0.05), inset 0px -15px 10px -12px rgba(0,0,0,0.05), inset 15px 10px -12px rgba(255,255,255,0.1);
color: #99a1a7;
}
.regular-checkbox:checked + label:after {
content: '\2714';
font-size: 14px;
position: absolute;
top: 0px;
left: 3px;
color: #99a1a7;
}
Demo: http://jsfiddle.net/tz4u38t3/