浏览器查看窗口中的中心div

时间:2011-08-25 13:08:41

标签: html css

我有一个div,我想在浏览器视图中垂直居中。

换句话说,如果用户向下滚动,我希望div显示在用户正在查看的当前Y轴中的相同中心位置。

由于

1 个答案:

答案 0 :(得分:2)

在这里,试试这个:

div {
    position: fixed;
    top: 50%;
    height: 100px;
    width: 200px;
    background: red;
    margin-top: -50px; /* half the div's height */
}
相关问题