2009-08-03 79 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="...">