2009-08-03 77 views
5

我有一个图像 - 一个加载图像。在页面中央显示图像

我希望该图像显示在页面中央。 我该怎么做?

我写的代码是:

img.loading 
    { 
     position:absolute; 
     left:0px; 
     top:0px; 
     z-index:1; 
    } 

我怎样才能使这个形象始终显示在页面的中心?

+0

这听起来像是你试图垂直居中。你能证实这一点吗?此外,你的形象是一个固定的高度? – 2009-08-03 20:24:23

+0

可能的解决方案[http://stackoverflow.com/questions/1207525/center-block-element-in-element/1207625#1207625](http://stackoverflow.com/questions/1207525/center-block-element-in -element/1207625#1207625) – desigeek 2009-08-03 20:25:54

回答

20

发现这一点:How to Center an Image Perfectly in CSS,可能的帮助。

#img 
{ 
    position:absolute; 
    width:592px; /*image width */ 
    height:512px; /*image height */ 
    left:50%; 
    top:50%; 
    margin-left:-296px; /*image width/2 */ 
    margin-top:-256px; /*image height/2 */ 
} 
6

W3C

IMG.displayed { 
    display: block; 
    margin-left: auto; 
    margin-right: auto 
} 

<IMG class="displayed" src="..." alt="...">