How To Center Checkbox Vertically In A Table-cell?
Solution 1:
The issue..
Checkbox elements, like other input elements are subject to vendor specific appearance
styling- you can override this by using appearance:none
or -webkit-appearance:none
etc..however this will then stop the checkbox from rendering at all.
As such, you have to 'live' with the default styling to some extent and implement a few overrides, for vertical alignment in the example you have given, you have to 'bump it up' by 1px, eg:
.category-item .input-container input {
margin:-1px 0 1px 0; /* <-- here */
padding: 0;
vertical-align: middle;
}
Demo Fiddle
The -moz-appearance [sic] CSS property is used in Gecko (Firefox) to display an element using a platform-native styling based on the operating system's theme.
Styling your own checkbox
A workaround is to 'replace' the checkbox element entirely, whilst still retaining its functionality- this can be done by adding a label
element then applying som nifty CSS, for example, the below:
Demo Fiddle
HTML
<div class="category-item">
<div class="input-container">
<input class='checkbox' id='checkbox' type="checkbox" />
<label for='checkbox' class='checkbox'></label>
</div>
</div>
CSS
.category-item {
display: table;
border: 1px solid #cccccc;
margin: 0;
padding: 0;
height:30px;
}
.category-item .input-container {
display: table-cell;
margin: 0;
padding: 0;
text-align: center;
vertical-align: middle;
height:30px;
}
label.checkbox {
position:relative;
vertical-align: middle;
border:1px solid black;
height:10px;
width:10px;
margin:0;
padding:0;
margin:-2px 0 2px 0;
line-height:1em;
padding:0;
overflow:hidden;
display:inline-block;
}
input.checkbox {
appearance:none;
height:0;
width:0;
overflow:hidden;
display:none;
margin:0;
padding:0;
-webkit-appearance:none;
}
input.checkbox:checked +label.checkbox:before {
content:'\2713';
position:absolute;
top:-3px;
font-size:10px;
left:2px;
}
Solution 2:
Adding display: block;
to the input will close up the vertical spacing. Otherwise, it's being treated as an inline item.
Solution 3:
use vertical-align: middle on input-container, not on the input tag itself
JSFiddle: http://jsfiddle.net/TZMF5/3/
.category-item .input-container {
display: table-cell;
margin: 0;
padding: 0;
text-align: center;
vertical-align: middle;
}
.category-item .input-container input {
margin: 0;
padding: 0;
}
Solution 4:
I am not sure if the next result is what you want. I only have commented two rules.
.category-item .input-container input {
/*margin: 0;*/
padding: 0;
/*vertical-align: middle;*/
}
The result is a centered box.
Post a Comment for "How To Center Checkbox Vertically In A Table-cell?"