有你需要解决两个方面。第一个方面是水平对齐。这可以通过边距很容易实现:自动应用于图像本身周围的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