As mentioned above, pseudo-elements do not work very well with inputs, however it would be nice to wrap the checkbox inside the label - it really is w3c, so it works as intended, and you donโt need to use the for tag for label, and also use the checkbox id for input.
Here is the HTML:
<label class="custom-checkbox"> <input type="checkbox" value="checkbox1"> <span>Checkbox</span> </label>
The code is quite universal, so if you only need a flag, without a label, you just leave the pool empty - you need to save the tag, although, or, alternatively, you can create your own custom class for applying pseudo-elements on the label itself.
Here is the CSS:
.custom-checkbox { position: relative; display: block; margin-top: 10px; margin-bottom: 10px; line-height: 20px; } .custom-checkbox span { display: block; margin-left: 20px; padding-left: 7px; line-height: 20px; text-align: left; } .custom-checkbox span::before { content: ""; display: block; position: absolute; width: 20px; height: 20px; top: 0; left: 0; background: #fdfdfd; border: 1px solid #e4e5e7; @include vendorize(box-shadow, inset 2px 2px 0px 0px rgba(0, 0, 0, 0.1)); } .custom-checkbox span::after { display: block; position: absolute; width: 20px; height: 20px; top: 0; left: 0; font-size: 18px; color: #0087b7; line-height: 20px; text-align: center; } .custom-checkbox input[type="checkbox"] { opacity: 0; z-index: -1; position: absolute; } .custom-checkbox input[type="checkbox"]:checked + span::after { font-family: "FontAwesome"; content: "\f00c"; background:#d44803; color:#fff; }
And here is the working fiddle: https://jsfiddle.net/ee1uhb3g/
Testing is tested in all browsers - FF, Chrome, Safari, IE, etc.
source share