2013-11-04 169 views
0

中心收藏我工作的一個收藏夾。我需要根據其內容動態調整大小。但我也需要它在屏幕中居中。我想是這樣的:動態高度

HTML:

<div class="lightbox-background"> 
    <div class="lightbox"> 
     LIGHTBOX CONTENT 
    </div> 
</div> 

CSS:

.lightbox-background { 
    background-color: rgba(0,0,0,0.9); 
    height: 100%; 
    left: 0; 
    position: fixed; 
    top: 0; 
    width: 100%; 
    z-index: 50; 
} 

.lightbox { 
    background-color: white; 
    width: 780px; 
    z-index: 100; 
    position: absolute; 
    left: 0; 
    right: 0; 
    top: 0; 
    bottom: 0; 
    margin: auto; 
    height: auto !important; 
    max-height: 90%; 
} 

我不能使它工作。如果可能的話,我想避免使用JS。我該怎麼做?

回答

1

您可以用vertical-align: middle還有:before選擇父容器上工作。看看我的小提琴:

http://jsfiddle.net/GA5K3/2/

+0

過了一段時間,直到我明白爲什麼它的工作原理。謝謝您的幫助。 您還可以設置 「FONT-SIZE:0;」到容器和「字體大小:14px的」,以除去內聯塊元件之間的空白空間,以它的孩子。如果您的容器太小,額外的空白可能會導致未經處理的結果。 –

0

我知道用CSS垂直居中的最好方法是將絕對位置頂部50%,然後設置頂部邊距negitave元素的一半高度。

既然你不知道你將不得不使用JS的高度。

也許有人有更好的技術。