Anna Marie
Anna Marie

Reputation: 91

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...

Upvotes: 5

Views: 105

Answers (3)

Kamalesh M. Talaviya
Kamalesh M. Talaviya

Reputation: 1420

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>

Upvotes: 3

Marcus H
Marcus H

Reputation: 1

You might want to consider wrapping your <input> in the <label> fields.

Example:

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

This implementation will increase accessibility and usability of your inputs and doesn't require any additional css.

Upvotes: 0

jo_va
jo_va

Reputation: 13963

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>

Upvotes: 4

Related Questions