center radio button below label
Asked Answered
M

3

10

Let's say I have some radio buttons with their labels looking like this:

<label for="my_radio_button_id">My Label</label>
<input type="radio" name="radio" id="my_radio_button_id" />

How do I center each radio button below its corresponding label and align it horizontally?

Mirabella answered 6/6, 2012 at 4:44 Comment(0)
S
27

FIDDLE

.checkboxgroup {
  display: inline-block;
  text-align: center;
}
.checkboxgroup label {
  display: block;
}
<div id="checkboxes">
  <div class="checkboxgroup">
    <label for="my_radio_button_id1">My Label1</label>
    <input type="radio" name="radio" id="my_radio_button_id1" />
  </div>
  <div class="checkboxgroup">
    <label for="my_radio_button_id2">My Label2</label>
    <input type="radio" name="radio" id="my_radio_button_id2" />
  </div>
  <div class="checkboxgroup">
    <label for="my_radio_button_id3">My Label3</label>
    <input type="radio" name="radio" id="my_radio_button_id3" />
  </div>
</div>
Sunless answered 6/6, 2012 at 4:59 Comment(1)
Exactly what I was thinking, the key is to wrap it up in a parent container such as a div and set proper display. +1upJalousie
J
2

Would this work? http://jsfiddle.net/fFEwh/2/

Jalousie answered 6/6, 2012 at 4:50 Comment(2)
Yeah but if I have many, they are not horizontally aligned. How do I horizontally align these jsfiddle.net/fFEwh/1Mirabella
updated, let me know if that is what you are trying to achieveJalousie
E
1

JSFIDDLE

preview

This alternative does not use div as wrappers, I use this to get a short DOM tree.

/* center radio below label */

.radioGroupBelow label {
  display: inline-block;
  text-align: center;
  margin: 0 0.2em;
}
.radioGroupBelow label input[type="radio"] {
  display: block;
  margin: 0.5em auto;
}
​
<div class="radioGroupBelow">
  Fruits:

  <label for="fruit1">Orange
    <input type="radio" name="fruits" id="fruit1">
  </label>

  <label for="fruit2">Apple
    <input type="radio" name="fruits" id="fruit2">
  </label>

  <label for="fruit3">Grape
    <input type="radio" name="fruits" id="fruit3">
  </label>

  <label for="fruit4">Lemon
    <input type="radio" name="fruits" id="fruit4">
  </label>
</div>
Equilibrant answered 25/9, 2012 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.