中心网站在屏幕

问题描述:

中心我正在开发一个网站,并希望集中在本站的画面(水平和垂直),但不能中心。 我只能水平居中。中心网站在屏幕

因为我一直在寻找,我找到了解决方案,当我尝试将其应用到我的网站,这一切都错位。

这是网站http://amsdarquitetura.com.br/

+0

您的网页上存在一些错误,例如具有相同名称的多个ID:s。尝试先解决它们:http://validator.w3.org/ – 2011-05-17 19:23:17

这是你所需要的:

<style type="text/css"> 

#global { 
    position:absolute; 
    width: 700px; 
    height: 400px; 
    margin-top: -200px; /* half of the height */ 
    margin-left: -350px; /* half of the width */ 
    left: 50%; 
    top: 50%; 

    border: 1px solid #333; 
    background-color: pink; 
} 

</style> 

将这个对你的父母DIV。

水平很简单:

<style type="text/css"> 
    #wrap { 
     margin: 0 auto; 
     width: 800px; 
    } 
</style> 

<body> 
    <div id="wrap"> 
     [content] 
    </div> 
</body> 

垂直要困难得多,并且可能需要CSS3或JavaScript的定位。

带领你正确的道路,尝试使用JavaScript(可能要添加一个标签)来检测window.height。

// The window.innerHeight and document.body.clientHeight will make sure it is cross-browser compatible. 
function getHeight() { 
    return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null; 
} 

然后减去你的主div的高度。

var maindiv=500; //this is your total height of main div with content 
var windowHeight = getHeight(); 
var displayHeight= windowHeight-maindiv; 

然后由图2(顶部/底部)划分

var topMargin= displayHeight/2; 

最后,一个 '边距' 添加到主分区。

document.getElementById('wrap').style.marginTop=topMargin; 

您必须定义整个站点的高度和宽度,以使其在两个方向上保持居中。

从那里,它只是在做你的垂直居中的数学。水平是非常自动的,定义一个宽度和边距:0 auto; (不要使用5px自动。)。

这里有垂直居中像样的文章:

http://blog.themeforest.net/tutorials/vertical-centering-with-css/

有是一个jQuery插件来做到这一点。我看到你已经在你的主页上使用了jQuery。

http://www.seodenver.com/simple-vertical-align-plugin-for-jquery/

看来这样做的CSS选择是复杂的,不可靠的,哈克,因为你发现了。

您可以实现自定义的jQuery函数做同样的事情。但是,为什么在其他人已经解决问题时维护该代码呢?