有你需要解決兩個方面。第一個方面是水平對齊。這可以通過邊距很容易實現:自動應用於圖像本身周圍的div元素。 DIV需要將寬度和高度設置爲圖像大小(否則這將不起作用)。要實現垂直中心對齊,您需要在HTML中添加一些JavaScript。這是因爲在頁面啓動時不知道HTML高度大小,並且稍後可能會更改。最好的解決方案是使用jQuery並編寫以下腳本: $(window)。ready(function(){/ *偵聽窗口就緒事件 - 在頁面加載後觸發*/ repositionCenteredImage(); });
$(window).resize(function() { /* listen to page resize event - in case window size changes*/
repositionCenteredImage();
});
function repositionCenteredImage() { /* reposition our image to the center of the window*/
pageHeight = $(window).height(); /*get current page height*/
/*
* calculate top and bottom margin based on the page height
* and image height which is 300px in my case.
* We use half of it on both sides.
* Margin for the horizontal alignment is left untouched since it is working out of the box.
*/
$("#pageContainer").css({"margin": (pageHeight/2 - 150) + "px auto"});
}
這是顯示圖像
HTML頁面看起來像這樣:
<body>
<div id="pageContainer">
<div id="image container">
<img src="brumenlabLogo.png" id="logoImage"/>
</div>
</div>
</body>
CSS連接的元素看起來是這樣的:
#html, body {
margin: 0;
padding: 0;
background-color: #000;
}
#pageContainer { /*css for the whole page*/
margin: auto auto; /*center the whole page*/
width: 300px;
height: 300px;
}
#logoImage { /*css for the logo image*/
width: 300px;
height: 300px;
}
您可以從我們公司下載的整體解決方案主頁在以下網址:
http://brumenlab.com
爲什麼要使用4個標籤,而不是2? 應該可以工作。 – Vivien 2012-04-24 10:39:04
我看到圖像底部的div,X rapresent的東西或我必須設置它的一些值? – CeccoCQ 2012-04-24 10:42:41
X對你來說可能是很好的:「5%」,「10px,0」......你也可以在底部圖像上加上「背景圖像」和「背景位置」 – Vivien 2012-04-24 10:44:30