这是困扰我的CSS小问题之一。

Stack Overflow周围的人如何在浏览器中始终垂直对齐复选框及其标签?

每当我在Safari中正确对齐它们(通常在输入上使用垂直对齐:基线),它们在Firefox和IE中就完全关闭了。

在Firefox中修复它,Safari和IE不可避免地被搞砸了。我每次编写表单时都在这上面浪费时间。

以下是我使用的标准代码:

<表单><div><label><input-type=“checkbox”/>标签文本</label></div></form>

我通常使用埃里克·迈耶的重置,所以表单元素相对来说没有覆盖。期待您提供的任何提示或技巧!


当前回答

如果您使用的是Twitter Bootstrap,您可以只使用<label>上的复选框类:

<label class="checkbox">
    <input type="checkbox"> Remember me
</label>

其他回答

这对我来说很好:

字段集{文本对齐:左侧;边框:无}字段集ol、字段集ul{填充:0;列表样式:无}字段集li{衬垫底部:1.5em;float:无;清除:左侧}标签{浮动:左侧;宽度:7em;右边距:1em}字段集复选框li{清晰:两者都有;衬垫:.75em}字段集复选框标签{边距:0 0 0 1em;宽度:20em}fieldset.checkboxes输入{浮动:左侧}<表单><fieldset class=“checkbox”><ul><li><input type=“checkbox”name=“happy”value=“yep”id=“happy“/><label for=“happy”>快乐</标签></li><li><input type=“checkbox”name=“饥饿”value=“yep”id=“饥饿”/>饿了吗</标签></li></ul></fieldset></form>

我发现唯一能将它们对齐的方法是使用绝对位置作为输入。摆弄输入的顶部变量得到了我想要的结果。

div {
  clear:    both;
  float:    none;
  position: relative;
}

input {
  left:     5px;
  position: absolute;
  top:      3px;
}

label {
  display:     block;
  margin-left: 20px;
}

我通常使用线条高度来调整静态文本的垂直位置:

标签{线条高度:18px;}输入{宽度:13px;高度:18px;字体大小:12px;线条高度:12px;}<表单><div><label><input-type=“checkbox”/>标签文本</label></div></form>

希望这会有所帮助。

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>

上面的代码将把你的列表项放在三列中,只需改变宽度即可。

对我来说,唯一完美可行的解决方案是:

input[type=checkbox], input[type=radio] {
    vertical-align: -2px;
    margin: 0;
    padding: 0;
}

今天在Chrome、Firefox、Opera、IE 7和8中进行了测试。示例:Fiddle