Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio/checkbox alignment in HTML/CSS

What is the cleanest way to align properly radio buttons / checkboxes with text? The only reliable solution which I have been using so far is table based:

<table> <tr>     <td><input type="radio" name="opt"></td>     <td>Option 1</td> </tr> <tr>     <td><input type="radio" name="opt"></td>     <td>Option 2</td> </tr> </table> 

This may be frown upon by some. I’ve just spent some time (again) investigating a tableless solution but failed. I’ve tried various combinations of floats, absolute/relative positioning and similar approaches. Not only that they mostly relied silently on an estimated height of the radio buttons / checkboxes, but they also behaved differently in different browsers. Ideally, I would like to find a solution which does not assume anything about sizes or special browser quirks. I’m fine with using tables, but I wonder where there is another solution.

like image 482
Jan Zich Avatar asked Dec 28 '08 17:12

Jan Zich


People also ask

How do I put radio buttons side by side?

To make a horizontal radio button set, add the data-type="horizontal" to the fieldset . The framework will float the labels so they sit side-by-side on a line, hide the radio button icons and only round the left and right edges of the group.

How do I center align radio buttons in HTML?

Just remove all styles but text-align:center and you got it. No need to display: inline-block. Simplest solution, but does not keep radio labels spaced apart with margin. But gets to the point that all that is needed is to text-align the fieldset.


2 Answers

I think I have finally solved the problem. One commonly recommended solution is to use vertical-align: middle:

<input type="radio" style="vertical-align: middle"> Label 

The problem, however, is that this still produces visible misalignments even though it should theoretically work. The CSS2 specification says that:

vertical-align: middle: Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.

So it should be in the perfect centre (the x-height is the height of the character x). However, the problem seems to be caused by the fact browsers commonly add some random uneven margins to radio buttons and checkboxes. One can check, for instance in Firefox using Firebug, that the default checkbox margin in Firefox is 3px 3px 0px 5px. I'm not sure where it comes from, but the other browsers seem to have similar margins as well. So to get a perfect alignment, one needs to get rid of these margins:

<input type="radio" style="vertical-align: middle; margin: 0px;"> Label 

It is still interesting to note that in the table based solution the margins are somehow eaten and everything aligns nicely.

like image 176
Jan Zich Avatar answered Oct 06 '22 17:10

Jan Zich


The following works in Firefox and Opera (sorry, I do not have access to other browsers at the moment):

<div class="form-field">     <input id="option1" type="radio" name="opt"/>     <label for="option1">Option 1</label> </div> 

The CSS:

.form-field * {     vertical-align: middle; } 
like image 28
sids Avatar answered Oct 06 '22 17:10

sids