How to align checkboxes and their labels consistently cross-browsers

前端 未结 30 1787
有刺的猬
有刺的猬 2020-11-22 05:56

This is one of the minor CSS problems that plagues me constantly. How do folks around Stack Overflow vertically align checkboxes and

相关标签:
30条回答
  • 2020-11-22 06:22

    Simple solution:

    <label style="display:block">
      <input style="vertical-align:middle" type="checkbox">
      <span  style="vertical-align:middle">Label</span>
    </label>
    

    Tested in Chrome, Firefox, IE9+, Safari, iOS 9+

    0 讨论(0)
  • 2020-11-22 06:23

    Warning! This answer is too old and doesn't work on modern browsers.

    I'm not the poster of this answer, but at the time of writing this, this is the most voted answer by far in both positive and negative votes (+1035 -17), and it's still marked as accepted answer (probably because the original poster of the question is the one who wrote this answer).

    As already noted many times in the comments, this answer does not work on most browsers anymore (and seems to be failing to do that since 2013).


    After over an hour of tweaking, testing, and trying different styles of markup, I think I may have a decent solution. The requirements for this particular project were:

    1. Inputs must be on their own line.
    2. Checkbox inputs need to align vertically with the label text similarly (if not identically) across all browsers.
    3. If the label text wraps, it needs to be indented (so no wrapping down underneath the checkbox).

    Before I get into any explanation, I'll just give you the code:

    label {
      display: block;
      padding-left: 15px;
      text-indent: -15px;
    }
    input {
      width: 13px;
      height: 13px;
      padding: 0;
      margin:0;
      vertical-align: bottom;
      position: relative;
      top: -1px;
      *overflow: hidden;
    }
    <form>
      <div>
        <label><input type="checkbox" /> Label text</label>
      </div>
    </form>

    Here is the working example in JSFiddle.

    This code assumes that you're using a reset like Eric Meyer's that doesn't override form input margins and padding (hence putting margin and padding resets in the input CSS). Obviously in a live environment you'll probably be nesting/overriding stuff to support other input elements, but I wanted to keep things simple.

    Things to note:

    • The *overflow declaration is an inline IE hack (the star-property hack). Both IE 6 and 7 will notice it, but Safari and Firefox will properly ignore it. I think it might be valid CSS, but you're still better off with conditional comments; just used it for simplicity.
    • As best I can tell, the only vertical-align statement that was consistent across browsers was vertical-align: bottom. Setting this and then relatively positioning upwards behaved almost identically in Safari, Firefox and IE with only a pixel or two of discrepancy.
    • The major problem in working with alignment is that IE sticks a bunch of mysterious space around input elements. It isn't padding or margin, and it's damned persistent. Setting a width and height on the checkbox and then overflow: hidden for some reason cuts off the extra space and allows IE's positioning to act very similarly to Safari and Firefox.
    • Depending on your text sizing, you'll no doubt need to adjust the relative positioning, width, height, and so forth to get things looking right.

    Hope this helps someone else! I haven't tried this specific technique on any projects other than the one I was working on this morning, so definitely pipe up if you find something that works more consistently.


    Warning! This answer is too old and doesn't work on modern browsers.

    0 讨论(0)
  • 2020-11-22 06:23

    This works well for me:

    fieldset {
      text-align:left;
      border:none
    }
    fieldset ol, fieldset ul {
      padding:0;
      list-style:none
    }
    fieldset li {
      padding-bottom:1.5em;
      float:none;
      clear:left
    }
    label {
      float:left;
      width:7em;
      margin-right:1em
    }
    fieldset.checkboxes li {
      clear:both;
      padding:.75em
    }
    fieldset.checkboxes label {
      margin:0 0 0 1em;
      width:20em
    }
    fieldset.checkboxes input {
      float:left
    }
    <form>
      <fieldset class="checkboxes">
        <ul>
          <li>
            <input type="checkbox" name="happy" value="yep" id="happy" />
            <label for="happy">Happy?</label>
          </li>
          <li>
            <input type="checkbox" name="hungry" value="yep" id="hungry" />
            <label for="hungry">Hungry?</label>
          </li>
        </ul>
      </fieldset>
    </form>

    0 讨论(0)
  • 2020-11-22 06:24

    Now that flexbox is supported in all modern browsers, something like this seems like an easier approach to me.

    <style>
    label {
      display: flex;
      align-items: center;
    }
    input[type=radio], input[type=checkbox]{
      flex: none;
    }
    </style>
    <form>
      <div>
        <label><input type="checkbox" /> Label text</label>
      </div>
    </form>
    


    Here's the complete prefixed version demo:

    label {
    	display: -webkit-box;
    	display: -webkit-flex;
    	display: -ms-flexbox;
    	display: flex;
    	-webkit-box-align: center;
    	-webkit-align-items: center;
    	-ms-flex-align: center;
    	align-items: center;
    }
    input[type=radio], 
    input[type=checkbox] {
    	-webkit-box-flex: 0;
    	-webkit-flex: none;
    	-ms-flex: none;
    	flex: none;
    	margin-right: 10px; 
    }
    /* demo only (to show alignment) */
    form {
      max-width: 200px
    }
    <form>
      <label>
        <input type="radio" checked>
        I am an aligned radio and label
      </label>
      <label>
          <input type="checkbox" checked>
        I am an aligned checkbox and label
      </label>
    </form>

    0 讨论(0)
  • 2020-11-22 06:26

    I usually leave a checkbox unlabeled and then make its "label" a separate element. It's a pain, but there's so much cross-browser difference between how checkboxes and their labels are displayed (as you've noticed) that this is the only way I can come close to controlling how everything looks.

    I also end up doing this in winforms development, for the same reason. I think the fundamental problem with the checkbox control is that it is really two different controls: the box and the label. By using a checkbox, you're leaving it up to the implementers of the control to decide how those two elements are displayed next to each other (and they always get it wrong, where wrong = not what you want).

    I really hope someone has a better answer to your question.

    0 讨论(0)
  • 2020-11-22 06:26

    CSS:

    .threeCol .listItem {
        width:13.9em;
        padding:.2em;
        margin:.2em;
        float:left;
        border-bottom:solid #f3f3f3 1px;
    }
    .threeCol input {
        float:left;
        width:auto;
        margin:.2em .2em .2em 0;
        border:none;
        background:none;
    }
    .threeCol label {
        float:left;
        margin:.1em 0 .1em 0;
    }
    

    HTML:

    <div class="threeCol">
        <div class="listItem">
            <input type="checkbox" name="name" id="id" value="checkbox1" />
            <label for="name">This is your checkBox</label>
        </div>
    </div>
    

    The above code will place your list items in threecols and just change widths to suit.

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