2016-06-16 46 views
0

我使用柔性盒造型製作了一個響應式燈箱,並且此燈箱中的圖像不適合容器。 (如果有足夠的垂直空間,它工作得很好)。燈箱中的圖像掛在容器外部

下面是可視化的問題,一個形象: enter image description here

那的HTML代碼:

<div id="dialog-container"> 
    <div id="dialog"> 
    <div id="dialog-content"> 
     <div class="image-wrapper"> 
     <img src="https://spotwild.org/css/images/dist/header/header-07-1600.jpg"> 
     </div> 
     <div class="thumbs"> 
     Here are the thumbs 
     </div> 
    </div> 
    </div> 
</div> 

CSS:

* { 
    margin: 0; 
    padding: 0; 
} 

html, body { 
    height: 100%; 
    width: 100%; 
} 

#dialog-container { 
    position: absolute; 
    width: 100%; 
    height: 100%; 
    background: black; 
    display: flex; 
    justify-content: center; 
    align-items: center; 
} 

#dialog { 
    max-width: 80%; 
    max-height: 80%; 
    background: white; 
    padding: 50px; 
    box-sizing: border-box; 
    position: relative; 
} 

#dialog-content { 
    display: flex; 
    flex: 1 1 100%; 
    flex-direction: column; 
    box-sizing: border-box; 
    height: 100%; 
    max-height: 100%; 
} 

.image-wrapper { 
    display: flex; 
    flex: 1 1 auto; 
    justify-content: center; 
    align-items: center; 
    min-width: 0; 
    min-height: 0; 
} 

img { 
    min-width: 0; 
    min-height: 0; 
    max-height: 100%; 
    max-width: 100%; 
    display: flex; 
    flex: 0 0 auto; 
} 

.thumbs { 
    background: #eee; 
    padding: 20px; 
    line-height: 25px; 
    box-sizing: border-box; 
} 

這裏是相應的jsfiddle: https://jsfiddle.net/ppkzt7m6/1/

有人有解決方案,並解釋爲什麼會發生這種情況?

+0

它從你的屏幕截圖,看起來你正在使用燈箱1版。如果是這樣,我會強烈建議使用[燈箱版本2](http://lokeshdhakar.com/projects/lightbox2/) – Martin

+0

無,我根本沒有使用任何圖書館 – friedi

+0

燈箱做你正在試圖建立的,有點重新發明輪子... – Martin

回答

0

附加高度#dialog

#dialog { 
    background: white none repeat scroll 0 0; 
    box-sizing: border-box; 
    height: 100%; 
    max-height: 80%; 
    max-width: 80%; 
    padding: 50px; 
    position: relative; 
} 
+0

我已經添加了高度,但現在我已經得到了問題,圖像不會調整:https://jsfiddle.net/ppkzt7m6/3/ – friedi

0

使用此功能。

img { 
    display: block; 
    height: 100%; 
    max-height: 100%; 
    max-width: 100%; 
    min-height: 0; 
    min-width: 0; 
    width: 100%; 
} 
#dialog { 
    background: #ffffff none repeat scroll 0 0; 
    box-sizing: border-box; 
    height: 100%; 
    max-height: 80%; 
    max-width: 80%; 
    padding: 50px; 
    position: relative; 
} 
+0

這不適合我工作:https://jsfiddle.net/ppkzt7m6/2/ – friedi

+0

但它工作正常me.may我知道確切的問題? – iyyappan

+0

正如你可以在jsfiddle中看到的那樣,圖像的高度不再改變。 – friedi

1

的重要組成部分,是對所有爲<img>元素,使百分比高度正常工作的父容器定義height

並在<img>元素上使用object-fit: cover;。請注意,目前的IE11和Edge不支持它,但在所有其他現代瀏覽器上都可以正常工作,請參閱support tables

jsFiddle

你也可以不用Flexbox的,關鍵是圖片標題設置爲絕對位置

jsFiddle

我建議如果你使用的背景圖片需要支持IE,例如:

jsFiddle

<div class="image-wrapper" style="background: url('https://spotwild.org/css/images/dist/header/header-07-1600.jpg') center/cover;"> 
+0

+1感謝您的回答!這不完全是我想要的。是否真的沒有解決方案使Flexbox和可變容器大小的工作? – friedi

+0

我上面更新了它,讓我知道如果有幫助。 – Stickers

+0

任何建議在編輯後是否有效? – Stickers