It is possible to implement custom bootstrap checkbox for the most popular browsers nowadays.
You can check my Bootstrap-Checkbox project in GitHub, which contains simple .less file. There is a good article in MDN describing some techniques, where the two major are:
Label redirects a click event.
Label can redirect a click event to its target if it has the for
attribute like in <label for="target_id">Text</label> <input id="target_id" type="checkbox" />
, or if it contains input as in Bootstrap case: <label><input type="checkbox" />Text</label>
.
It means that it is possible to place a label in one corner of the browser, click on it, and then the label will redirect click event to the checkbox located in other corner producing check/uncheck action for the checkbox.
We can hide original checkbox visually, but make it is still working and taking click event from the label. In the label itself we can emulate checkbox with a tag or pseudo-element :before :after
.
General non supported tag for old browsers
Some old browsers does not support several CSS features like selecting siblings p+p
or specific search input[type=checkbox]
. According to the MDN article browsers that support these features also support :root
CSS selector, while others not. The :root
selector just selects the root element of a document, which is html
in a HTML page. Thus it is possible to use :root
for a fallback to old browsers and original checkboxes.
Final code snippet:
:root {_x000D_
/* larger checkbox */_x000D_
}_x000D_
:root label.checkbox-bootstrap input[type=checkbox] {_x000D_
/* hide original check box */_x000D_
opacity: 0;_x000D_
position: absolute;_x000D_
/* find the nearest span with checkbox-placeholder class and draw custom checkbox */_x000D_
/* draw checkmark before the span placeholder when original hidden input is checked */_x000D_
/* disabled checkbox style */_x000D_
/* disabled and checked checkbox style */_x000D_
/* when the checkbox is focused with tab key show dots arround */_x000D_
}_x000D_
:root label.checkbox-bootstrap input[type=checkbox] + span.checkbox-placeholder {_x000D_
width: 14px;_x000D_
height: 14px;_x000D_
border: 1px solid;_x000D_
border-radius: 3px;_x000D_
/*checkbox border color*/_x000D_
border-color: #737373;_x000D_
display: inline-block;_x000D_
cursor: pointer;_x000D_
margin: 0 7px 0 -20px;_x000D_
vertical-align: middle;_x000D_
text-align: center;_x000D_
}_x000D_
:root label.checkbox-bootstrap input[type=checkbox]:checked + span.checkbox-placeholder {_x000D_
background: #0ccce4;_x000D_
}_x000D_
:root label.checkbox-bootstrap input[type=checkbox]:checked + span.checkbox-placeholder:before {_x000D_
display: inline-block;_x000D_
position: relative;_x000D_
vertical-align: text-top;_x000D_
width: 5px;_x000D_
height: 9px;_x000D_
/*checkmark arrow color*/_x000D_
border: solid white;_x000D_
border-width: 0 2px 2px 0;_x000D_
/*can be done with post css autoprefixer*/_x000D_
-webkit-transform: rotate(45deg);_x000D_
-moz-transform: rotate(45deg);_x000D_
-ms-transform: rotate(45deg);_x000D_
-o-transform: rotate(45deg);_x000D_
transform: rotate(45deg);_x000D_
content: "";_x000D_
}_x000D_
:root label.checkbox-bootstrap input[type=checkbox]:disabled + span.checkbox-placeholder {_x000D_
background: #ececec;_x000D_
border-color: #c3c2c2;_x000D_
}_x000D_
:root label.checkbox-bootstrap input[type=checkbox]:checked:disabled + span.checkbox-placeholder {_x000D_
background: #d6d6d6;_x000D_
border-color: #bdbdbd;_x000D_
}_x000D_
:root label.checkbox-bootstrap input[type=checkbox]:focus:not(:hover) + span.checkbox-placeholder {_x000D_
outline: 1px dotted black;_x000D_
}_x000D_
:root label.checkbox-bootstrap.checkbox-lg input[type=checkbox] + span.checkbox-placeholder {_x000D_
width: 26px;_x000D_
height: 26px;_x000D_
border: 2px solid;_x000D_
border-radius: 5px;_x000D_
/*checkbox border color*/_x000D_
border-color: #737373;_x000D_
}_x000D_
:root label.checkbox-bootstrap.checkbox-lg input[type=checkbox]:checked + span.checkbox-placeholder:before {_x000D_
width: 9px;_x000D_
height: 15px;_x000D_
/*checkmark arrow color*/_x000D_
border: solid white;_x000D_
border-width: 0 3px 3px 0;_x000D_
}
_x000D_
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>_x000D_
<p>_x000D_
Original checkboxes:_x000D_
</p>_x000D_
<div class="checkbox">_x000D_
<label class="checkbox-bootstrap"> _x000D_
<input type="checkbox"> _x000D_
<span class="checkbox-placeholder"></span> _x000D_
Original checkbox_x000D_
</label>_x000D_
</div>_x000D_
<div class="checkbox">_x000D_
<label class="checkbox-bootstrap"> _x000D_
<input type="checkbox" disabled> _x000D_
<span class="checkbox-placeholder"></span> _x000D_
Original checkbox disabled_x000D_
</label>_x000D_
</div>_x000D_
<div class="checkbox">_x000D_
<label class="checkbox-bootstrap"> _x000D_
<input type="checkbox" checked> _x000D_
<span class="checkbox-placeholder"></span> _x000D_
Original checkbox checked_x000D_
</label>_x000D_
</div>_x000D_
<div class="checkbox">_x000D_
<label class="checkbox-bootstrap"> _x000D_
<input type="checkbox" checked disabled> _x000D_
<span class="checkbox-placeholder"></span> _x000D_
Original checkbox checked and disabled_x000D_
</label>_x000D_
</div>_x000D_
<div class="checkbox">_x000D_
<label class="checkbox-bootstrap checkbox-lg"> _x000D_
<input type="checkbox"> _x000D_
<span class="checkbox-placeholder"></span> _x000D_
Large checkbox unchecked_x000D_
</label>_x000D_
</div>_x000D_
<br/>_x000D_
<p>_x000D_
Inline checkboxes:_x000D_
</p>_x000D_
<label class="checkbox-inline checkbox-bootstrap">_x000D_
<input type="checkbox">_x000D_
<span class="checkbox-placeholder"></span>_x000D_
Inline _x000D_
</label>_x000D_
<label class="checkbox-inline checkbox-bootstrap">_x000D_
<input type="checkbox" disabled>_x000D_
<span class="checkbox-placeholder"></span>_x000D_
Inline disabled_x000D_
</label>_x000D_
<label class="checkbox-inline checkbox-bootstrap">_x000D_
<input type="checkbox" checked disabled>_x000D_
<span class="checkbox-placeholder"></span>_x000D_
Inline checked and disabled_x000D_
</label>_x000D_
<label class="checkbox-inline checkbox-bootstrap checkbox-lg">_x000D_
<input type="checkbox" checked>_x000D_
<span class="checkbox-placeholder"></span>_x000D_
Large inline checked_x000D_
</label>
_x000D_