Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to vertically align a html radio button to it's label?

I have a form with radio buttons that are on the same line as their labels. The radio buttons are however not aligned vertically with their labels as shown in the screenshot below.

The radio buttons.

How can I vertically align the radio buttons with their labels?

Edit:

Here's the html code:

<input checked="checked" type="radio" name="user_level" id="rd1" value="1"/> <label for="rd1">radio 1</label><br/> <input type="radio" name="user_level" id="rd2" value="2"/> <label for="rd2">radio 2</label><br/> <input type="radio" name="user_level" id="rd3" value="3"/> <label for="rd3">radio 3</label><br/> 

And the css code:

label{     padding:5px;     color:#222;     font-family:corbel,sans-serif;     font-size: 14px;     margin: 10px; } 
like image 549
ninjacoder Avatar asked Nov 22 '12 09:11

ninjacoder


People also ask

How do I link a radio button to a label in HTML?

To label a radio button, add a <label> element after the <input> element and insert a for attribute with the same value as the id of the associated <input> element. Then, write your label text in the <label> tag.

How do you vertically align labels?

How to center text vertically in a regular label control. The CSS tag vertical-align does not work but there is a work-around of sorts...for some reason padding-top seems to work. So you can set a value for the padding-top and then adjust the height of your label to get the text where you want it to be.

How do you align a button vertically?

Other than flexbox property, we can also center align the button horizontally and vertically using a set of CSS properties. Add position: relative to the container class and position: absolute to the class containing the button. Now use left:50% and top:50% to position the button to the center of the container.


2 Answers

Try this:

input[type="radio"] {   margin-top: -1px;   vertical-align: middle; } 
like image 96
zakangelle Avatar answered Sep 27 '22 20:09

zakangelle


I know I'd selected the anwer by menuka devinda but looking at the comments below it I concurred and tried to come up with a better solution. I managed to come up with this and in my opinion it's a much more elegant solution:

input[type='radio'], label{        vertical-align: baseline;     padding: 10px;     margin: 10px;  } 

Thanks to everyone who offered an answer, your answer didn't go unnoticed. If you still got any other ideas feel free to add your own answer to this question.

like image 41
ninjacoder Avatar answered Sep 27 '22 22:09

ninjacoder