这是困扰我的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>

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 {
    margin: 0;
}

真的很管用

我找到了一种在几乎所有浏览器中都能得到相同结果的方法。至少是最新的浏览器。它适用于Firefox、Chrome、Safari和Opera。在IE中,它看起来就像没有对输入或标签应用规则一样(即,它的标签仍然比输入低几个像素),所以我认为这是可以理解的。

HTML

<label class="boxfix"><input type="radio">Label</label>

CSS

.boxfix {
    vertical-align: bottom;
}
.boxfix input {
    margin: 0;
    vertical-align: bottom;
    position: relative;
    top: 1.999px; /* the inputs are slightly more centered in IE at 1px (they don't interpret the .999 here), and Opera will round it to 2px with three decimals */
}

我不喜欢相对定位,因为它使元素呈现在其级别上的所有其他元素之上(如果您有复杂的布局,它可以在某些元素之上)。

我发现垂直对齐:sub使复选框在Chrome、Firefox和Opera中看起来足够对齐。由于我没有MacOS,IE10稍有问题,所以无法检查Safari,但我发现它对我来说已经足够好了。

另一个解决方案可能是尝试为每个浏览器制作特定的CSS,并使用%/pixel/EM中的垂直对齐方式对其进行微调:http://css-tricks.com/snippets/css/browser-specific-hacks/