Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align input="radio" with their labels?

I know there are a lot answers here. I searched, tried to adapt but I failed all the time, that is why I am asking now.

I have the following problem. I want, both, labels and there inputs to be in the same line: Yes o No o (o symbolize the input).

Here my following code, remark that I changed the display property of label to inline block:

<div class="col-sm-3">
        <label>Yes</label>
        <input type="radio" name="yes/no" checked>
        <label>No</label>
        <input type="radio" name="yes/no" >
    </div> 

Sorry for answering, although there are a lot of similar questions and answers but adapting them did just not work...

like image 994
Anna Marie Avatar asked Mar 14 '19 10:03

Anna Marie


People also ask

How do you align inputs and labels?

We specify the margin-bottom of our <div> element. Then, we set the display of the <label> element to "inline-block" and give a fixed width. After that, set the text-align property to "right", and the labels will be aligned with the inputs on the right side.

How do I label an input type radio?

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.


2 Answers

If you want the inputs and the label to be 'bottom-aligned', try using vertical-align: text-bottom.

input[type='radio'] {
  vertical-align: text-bottom;
  margin-bottom: 1px;
}
div {
  display: inline-block;
  border: 1px solid black;
}
<div class="col-sm-3">
    <label>Yes</label>
    <input type="radio" name="yes/no" checked>
    <label>No</label>
    <input type="radio" name="yes/no" >
</div>
like image 133
jo_va Avatar answered Oct 18 '22 08:10

jo_va


Apply vertical-align: middle with margin-top: -1px to each input[type='radio'] :

input[type='radio'] {
  margin-top: -1px;
  vertical-align: middle;
}
<div class="col-sm-3">
    <label>Yes</label>
    <input type="radio" name="yes/no" checked>
    <label>No</label>
    <input type="radio" name="yes/no" >
</div>

Perfect center : just line added crossing text and button in this second snippet to show perfect center

input[type='radio'] {
  margin-top: -1px;
  vertical-align: middle;
}

.col-sm-3 {
    position: relative;
    border: 1px solid;
}

.col-sm-3:after {
    display: block;
    content: '';
    position: absolute;
    width: 100%;
    height: 1px;
    background: black;
    top: 50%;
}
<div class="col-sm-3">
    <label>Yes</label>
    <input type="radio" name="yes/no" checked>
    <label>No</label>
    <input type="radio" name="yes/no" >
</div>
like image 37
Kamalesh M. Talaviya Avatar answered Oct 18 '22 09:10

Kamalesh M. Talaviya