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

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

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

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

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

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

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


当前回答

位置:相对;在IE中的z索引和jQuery的slideUp/slideDown等动画存在一些问题。

CSS:

input[type=checkbox], input[type=radio] {
    vertical-align: baseline;
    position: relative;
    top: 3px;
    margin: 0 3px 0 0;
    padding: 0px;
}
input.ie7[type=checkbox], input.ie7[type=radio] {
    vertical-align: middle;
    position: static;
    margin-bottom: -2px;
    height: 13px;
    width: 13px;
}

jQuery:

$(document).ready(function () {
    if ($.browser.msie && $.browser.version <= 7) {
        $('input[type=checkbox]').addClass('ie7');
        $('input[type=radio]').addClass('ie7');
    }
});

样式可能需要根据<label>中使用的字体大小进行调整

PS(秒):我使用ie7js使css在IE6中工作。

其他回答

尝试我的解决方案,我在IE 6、FF2和Chrome中尝试过,它在所有三种浏览器中逐像素渲染。

* {填充:0px;边距:0px;}#世界银行{宽度:15px;高度:15px;浮动:左侧;}#某人标签{浮动:左侧;左侧填充:3px;}<div><input id=“wb”type=“checkbox”/><label for=“wb”id=“somelabel”>Web浏览器</label></div>

尝试垂直对齐:中间

而且你的代码看起来应该是:<表单><div><input id=“blah”type=“checkbox”><label for=“blah“>标签文本</label></div></form>

最简单的解决方案是

输入{垂直对齐:文本顶部;}

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

<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;
}

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

<fieldset class="checks">
    <legend>checks for whatevers</legend>
    <input type="" id="x" />
    <label for="x">Label</label>
    <input type="" id="y" />
    <label for="y">Label</label>
    <input type="" id="z" />
    <label for="z">Label</label>
</fieldset>

无论如何,您应该将分组在一起的表单控件包装在它们自己的字段集中,在这里,它播放包装。设置输入/标签显示:块,输入浮动左,标签浮动右,设置宽度,控制左右边距的间距,相应地对齐标签文本。

so

fieldset.checks {
    width:200px
}
.checks input, .checks label {
    display:block;
}
.checks input {
    float:right;
    width:10px;
    margin-right:5px
}
.checks label {
    float:left;
    width:180px;
    margin-left:5px;
    text-align:left;
    text-indent:5px
}

对于跨浏览器/媒体解决方案,您可能需要同时设置边框、轮廓和线条高度。