57

I have looked at the different questions regarding this issue, but couldn't find anything that works due to limitations in my markup.

My markup looks like so (unfortunately as this is generated by some backend, I am unable to change the markup).

<ul>        
    <li>
        <input type="checkbox" value="1" name="test[]" id="myid1">
        <label for="myid1">label1</label>
    </li>
    <li>
        <input type="checkbox" value="2" name="test[]" id="myid2">
        <label for="myid2">label1</label>
    </li>
</ul>

I need the checkbox to be on the right and centered vertically in the <li>

Currently, this is styled as:

li input{
   display: inline-block;
   float: right;
   margin-right: 10px;
}

I have tried using various values for vertical-align, but that doesn't seem to help. Also, in some cases the label can be very long and span multiple lines. The checkbox would still need to be able to vertically center itself when the height of the li is arbitrary.

How can I go about achieving this?

1
  • 1
    Would prefer not to :) Forgot to mention that the markup is generated by a backend, so I can't really use tables in my markup.
    – F21
    Commented Mar 22, 2012 at 1:42

7 Answers 7

76

Vertical alignment only works on inline elements. If you float it, then I don't think it is treated as part of that stream of inline elements any more.

Make the label an inline-block, and use vertical alignment on both the label and the input to align their middles. Then, assuming it is okay to have a specific width on the labels and checkboxes, use relative positioning instead of floating to swap them (jsFiddle demo):

input {
    width: 20px;

    position: relative;
    left: 200px;
         
    vertical-align: middle;
}

label {  
    width: 200px;
    
    position: relative;
    left: -20px;
    
    display: inline-block;
    vertical-align: middle;
}

2024 update, using Flexbox (fiddle):

ul {
  width: 300px; /* Only for demonstration. */
  border: 1px dashed gray;
}

li {
    display: flex;
    flex-direction: row-reverse; /* Only needed for swapping their order. */
    align-items: center;
}

input {
    background-color: blue;
}

label {
    background-color: red;
    flex-grow: 1; /* Grow the label to fill available space, pushing the checkboxes to the edge of the container. */
}

Less hacky, and with this you don't need to specify any widths. It will grow to fill the container, and wrap if necessary.

Another alternative would be Flexbox Grid.

2
  • 2
    Thanks so much, for me the key point was "use vertical alignment on both the label and the input", I was using it only on the input.. Thanks again!
    – Thierry J
    Commented May 22, 2014 at 1:27
  • The key to this is that the label width forces the wrapping. If the ul width forces the wrapping. Having a width on the label is mandatory.
    – boatcoder
    Commented Dec 29, 2014 at 13:29
13

Its not a perfect solution, but a good workaround.

You need to assign your elements to behave as table with display: table-cell

Solution: Demo

HTML:

<ul>        
    <li>
        <div><input type="checkbox" value="1" name="test[]" id="myid1"></div>
        <div><label for="myid1">label1</label></div>
    </li>
    <li>
        <div><input type="checkbox" value="2" name="test[]" id="myid2"></div>
        <div><label for="myid2">label2</label></div>
    </li>
</ul>

CSS:

li div { display: table-cell; vertical-align: middle; }
0
4

The most effective solution that I found is to define the parent element with display:flex and align-items:center

LIVE DEMO

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <style>
      .myclass{
        display:flex;
        align-items:center;
        background-color:grey;
        color:#fff;
        height:50px;
      }
    </style>
  </head>
  <body>
    <div class="myclass">
      <input type="checkbox">
      <label>do you love Ananas?
      </label>
    </div>
  </body>
</html>

OUTPUT:

enter image description here

1
  • did we use the height of parent? Commented Aug 5, 2022 at 13:11
1

Add CSS:


li {
  display: table-row;
 
 }
li div {
   display: table-cell;
   vertical-align: middle;

  }
.check{
  width:20px;

  }
ul{
   list-style: none;
  }
  
 <ul>
       <li>

           <div><label for="myid1">Subject1</label></div>
            <div class="check"><input type="checkbox" value="1"name="subject" class="subject-list" id="myid1"></div>
       </li>
       <li>

           <div><label for="myid2">Subject2</label></div>
              <div class="check" ><input type="checkbox" value="2"  class="subject-list" name="subjct" id="myid2"></div>
       </li>
   </ul>

0
<div>
    <input type="checkbox">
    <img src="/image.png" />
</div>
input[type="checkbox"]
{
    margin-top: -50%;
    vertical-align: middle;
}
0

This works reliably for me. Cell borders and height added for effect and clarity:

<table>
  <tr>
    <td style="text-align:right; border: thin solid; height:50px">Some label:</td>
    <td style="border: thin solid;">
      <input type="checkbox" checked="checked" id="chk1" style="cursor:pointer; "><label for="chk1" style="margin-top:auto; margin-left:5px; margin-bottom:auto; cursor:pointer;">Check Me</label>
    </td>
  </tr>
</table>

1
  • I'd recommend to separate inline CSS, otherwise the answer is difficult to follow
    – YakovL
    Commented Mar 21, 2019 at 12:49
-2

make input to block and float, Adjust margin top value.

HTML:

<div class="label">
<input type="checkbox" name="test" /> luke..
</div>

CSS:

/*
change margin-top, if your line-height is different.
*/
input[type=checkbox]{
height:18px;
width:18px;
padding:0;
margin-top:5px;
display:block;
float:left;
}
.label{
border:1px solid red;
}

Demo

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.