“vertical-align:middle”与Firefox中间不对齐

时间:2009-04-08 22:27:39

标签: css vertical-alignment

我正在尝试垂直对齐一些不同大小的文本,但是,Firefox会在中间显示较小的文本。

CSS:

div.categoryLinks {
    margin: 1em 16px;
    padding: 0 4px;
    border-width: 1px 0;
    border-style: solid;
    border-color: #ece754;
    background: #f7f5b7;
    color: #a49f1c;
    text-align: center;
    font-size: 1.4em;
}
div.categoryLinks em {
    font-size: 0.6em;
    font-style: normal;
    vertical-align: middle;
}

HTML:

<div class="categoryLinks">
    <em>SECTION:</em>
    Page One &#183;
    Page Two &#183;
    <a href="link">Page Three</a>
</div>

截图: screenshot
(来源:doheth.co.uk

更新:为了清楚起见,我知道vertical-align的工作方式或多或少,即我已经知道常见的误解。

从更多调查中,解决此问题的最简单方法似乎是将较大的文本包装在span中,并将vertical-align设置为{}}。然后.categoryLinks的两个孩子相互对齐。除非有人能在没有额外标记的情况下表现出更好的方式吗?

5 个答案:

答案 0 :(得分:15)

垂直对齐仅在表格单元格或内联块元素上按预期工作。如果您需要更多信息,建议您阅读Understanding vertical-align, or "How (Not) To Vertically Center Content"

编辑:你还有其他事情发生,因为这对我有用,就像在Firefox上一样。我甚至放弃了SECTION的字体大小:向下,它仍然居中。您是否使用过Firebug来查看其他CSS正在影响它?

这样可以:

<html>
<head>
<style type="text/css">
div.categoryLinks {
        margin: 1em 16px;
        padding: 0 4px;
        border-width: 1px 0;
        border-style: solid;
        border-color: #ece754;
        background: #f7f5b7;
        color: #a49f1c;
        text-align: center;
        font-size: 1.4em;
}
div.categoryLinks em {
        font-size: 0.4em;
        font-style: normal;
        vertical-align: middle;
}
</style>
</head>
<body>
<div class="categoryLinks">
        <em>SECTION:</em>
        Page One &#183;
        Page Two &#183;
        <a href="link">Page Three</a>
</div>
</body>

注意:部分字体大小从原来的0.6em改为0.4em以强调这一点。

答案 1 :(得分:2)

Firefox正确呈现。 vertical-align属性是内联的,这意味着它不适用于像&lt; div&gt;这样的块元素。 (和&lt; p&gt;等)。尝试添加 display:inline ,看看是否有效。

答案 2 :(得分:2)

垂直对齐只应用于内联块元素(我认为图像是默认情况下唯一具有此布局属性的东西),因此要使用它来定位内联元素,首先将其转换为内联块,然后你可以使用边距和填充来定位它,就像你正常的块元素一样:

div.categoryLinks {
        margin: 1em 16px;
        padding: 0 4px;
        border-width: 1px 0;
        border-style: solid;
        border-color: #ece754;
        background: #f7f5b7;
        color: #a49f1c;
        text-align: center;
        font-size: 1.4em;

}
div.categoryLinks em {
            font-size: 0.6em;

           display:inline-block;
        vertical-align:top;
        font-style: normal;
        padding: 2px 0 0 0;

}

但是你必须tweak it a little for firefox 2,但这是因为firefox不支持Web标准的一个例子。另一方面,由于很少有人仍然使用ffx2,所以你无法理解调整,这只是一个非常小的设计缺陷。

答案 3 :(得分:1)

我修复了这个问题,只是删除了:

 white-space: nowrap;
来自父div的

我不确定为什么但是添加了这条规则,Firefox不会应用:

vertical-align: middle;

答案 4 :(得分:0)

我遇到了同样的问题。这对我有用:

 <style type="text/css">
    div.parent {
         position: relative;
    }

    /*vertical middle and horizontal center align*/
    img.child {
        bottom: 0;
        left: 0;
        margin: auto;
        position: absolute;
        right: 0;
        top: 0;
    }
    </style>

    <div class="parent">
        <img class="child"> 
    </div>
相关问题