div不是水平放置的

时间:2010-12-26 22:38:31

标签: javascript jquery

我没有让我的表单元素在中心水平放置这里是我的代码

$('.form') 
.css('top', window.screen.availHeight / 2 - $('.form').height() / 2) 

2 个答案:

答案 0 :(得分:1)

首先,确保position: absolute;为@ Douglas指出。过去,您将获取屏幕高度,就像在整个显示器中一样,而不是浏览器窗口。而不是window.screen.availHeight,您可能只想要$(window).height(),如下所示:

$('.form').css('top', $(window).height() / 2 - $('.form').height() / 2);

You can test it out here

答案 1 :(得分:0)

使用css设置top属性不会影响元素的水平位置。

如果元素的位置设置为top,则设置left(或static进行水平定位)将不起作用,这是默认值。

尝试绝对位置:

.form {
    position: absolute;
    top: 100px;
}
相关问题