css - 从顶部,水平居中的div的绝对位置

cap*_*oke 9 css css-position centering

我看了googol谷歌搜索结果没有发现任何工作.

我需要让我的div(高度333像素,宽度550像素)水平居中,并始终从顶部275像素.每当我尝试这样做,它就会消失.

fux*_*xia 26

如果div应该在顶部,你必须使用position:absolute.否则,来自@sdleihssirhc的答案应该有效.

定位示例

#middlebox
{
    position:    absolute;
    top:         275px;
    left:        50%;    /* move the left edge to the center … */
    margin-left: -275px; /* … and move it to the left half the box’ width. */
    z-index:     9999;   /* Try to get it on top. */
}
Run Code Online (Sandbox Code Playgroud)

使用DragonflyFirebug等工具检查属性是否仍然消失.

  • @captainandcoke 550/2 = 275.;) (11认同)