Vertically aligning a checkbox

后端 未结 7 945
后悔当初
后悔当初 2020-12-29 19:52

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 (unfor

相关标签:
7条回答
  • 2020-12-29 20:20

    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>

    0 讨论(0)
  • 2020-12-29 20:27
    <div>
        <input type="checkbox">
        <img src="/image.png" />
    </div>
    
    input[type="checkbox"]
    {
        margin-top: -50%;
        vertical-align: middle;
    }
    
    0 讨论(0)
  • 2020-12-29 20:30

    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 讨论(0)
  • 2020-12-29 20:32

    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;
    }
    
    0 讨论(0)
  • 2020-12-29 20:33

    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 讨论(0)
  • 2020-12-29 20:35

    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:

    0 讨论(0)
提交回复
热议问题