2011-04-09 86 views

回答

1

如果圖像的尺寸是固定的(比如,寬度200高度300),那麼這樣的事情應該工作:

HTML:

<div id="table-and-image-container"> 
    <table id="the-table">...</table> 
    <img src="..." id="the-image" /> 
</div> 

CSS:

#table-and-image-container 
{ 
    position: relative; 
    display: inline-block; 
} 
#the-table 
{ 
    z-index: 1; 
} 
#the-image 
{ 
    left: 50%; 
    margin-left: -100px; /* = 200px width of image/2 */ 
    margin-top: -150px; /* = 300px height of image/2 */ 
    position: absolute; 
    top: 50%; 
    z-index: 2; 
} 

包含#table-and-image-container將是表格的大小,因爲position: absolute將採用#the-image超出尺寸算法和display: inline-block w生病將寬度減少到表的寬度而不是100%的容器。然後,您使用標準top/left/margin技巧將圖像放置在中心位置,並使用z-index確保它位於頂部。

+0

謝謝Domenic,但這沒有奏效。圖像出現在表格的垂直中心,但在頁面的水平中心(不是表格)上。看看這裏:http://jsfiddle.net/wxuwt/任何想法如何解決? – PleaseHelpMe 2011-04-09 11:28:18

+0

@NeedExpertHelp:對於'#table-and-image-container',添加'float:left'或'display:inline-block'。 – thirtydot 2011-04-09 13:00:37

+0

@NeedExpertHelp:確實,'display:inline-block'是要走的路;編輯我的答案以反映這一點。 http://jsfiddle.net/wxuwt/4/ – Domenic 2011-04-09 22:12:30