html - 使用负边距的垂直居中,在 Safari 中有效,但在 Firefox/Chrome 中无效,有什么想法吗?

标签 html css positioning center vertical-alignment

我一直在寻找与我有类似问题的人:

http://dominicburton.co.uk/soas/

我正在处理的网站在 Safari 中呈现良好,但在 Chrome 或 Firefox 中呈现不佳。

Firefox 看到了样式,但只渲染负的上边距而不是 top:50%。

#wrapper {
margin-top: -245px;
position: relative;
top: 50%;
}

有人知道为什么会这样吗?我的想法几乎用完了......

最佳答案

你需要使用绝对定位,添加一个高度和边距应该是高度的一半取反。在这里查看 http://jsfiddle.net/CYKwM/

#wrapper {
    height:490px; /* you need to specify a height*/
    width:490px;
    margin-top: -245px; /*negative half the height*/
    position: absolute; /*change to absolute*/
    top: 50%;
    background:red;
}

关于html - 使用负边距的垂直居中,在 Safari 中有效,但在 Firefox/Chrome 中无效,有什么想法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4986152/

相关文章:

jquery - 如何使用 jQuery 选择所有空的 p 标签?

html - 为什么我的网站背景图片的顶部和底部被截断(全屏图像背景)?

html - div 框内的 div 框。相对定位,等边距,四边间距

html - 无法让html元素在同一行

CSS 高度不是 100%

javascript - OnClick 事件表无法正确触发

html - 选择没有<a>的<li>

CSS3 FlexBox 在 Firefox 22 中无法正确显示

CSS 位置(无标签)和删除(隐藏)空 DIVS

javascript - JSON数据加载问题本地存储