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

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

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

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

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

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

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


当前回答

正如pokrishka已经建议的那样,简单地使用垂直对齐:sub。

不停摆弄

HTML代码:

<div class="checkboxes">
    <label for="check1">Test</label>
    <input id="check1" type="checkbox"></input>
</div>

CSS代码:

.checkboxes input {
    vertical-align: sub;
}

其他回答

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

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

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

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

输入类型复选框包装在标签内并向左浮动,如下所示:

<label for="id" class="checkbox">
    <input type="checkbox" id="id">
    <span>The Label</span>
</label>

这对我有用:

label.checkbox {
    display: block;
}
.checkbox input {
    float: left;
    height: 18px;
    vertical-align: middle;
}
.checkbox span {
    float: left;
    line-height: 18px;
    margin: 0 0 0 20px;
}

确保的高度与(块级别)的线高度相同。

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

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

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

输入[类型=复选框]{垂直对齐:中间;} <input id=“testCheckbox”name=“testCheckbox”type=“checkbox”><label for=“testCheckbox”>我应该对齐</label>

也许有些人也犯了和我一样的错误?这是。。。我为输入框设置了一个宽度,因为它们大多是“文本”类型,但后来忘记了覆盖复选框的宽度,所以我的复选框试图占据大量多余的宽度,因此很难在旁边对齐标签。

.checkboxlabel {
    width: 100%;
    vertical-align: middle;
}
.checkbox {
    width: 20px !important;
}
<label for='acheckbox' class='checkboxlabel'>
    <input name="acheckbox" id='acheckbox' type="checkbox" class='checkbox'>Contact me</label>

早在IE6(使用类选择器)以及Firefox、Safari和Chrome的最新版本中,提供了可点击的标签和正确的对齐方式