2015-04-19 22 views
1

我有一個定義的寬度和高度的div。然後我有一張我想要的照片(保持比例)。這樣的JsFiddle使img響應,並填入分區

<div style="width: 200px; height: 300px; background: red;"> 
    <img src="http://placehold.it/200x800" style="width: auto; height: auto; max-width: 100%; max-height: 100%" /> 
</div> 

現在我想纏繞在IMG(即恰好ajusted到IMG寬度和高度和位置),因爲那時希望有在div裏面的一些元素與position:absolute相對於一個div img。檢查JsFiddle

<div style="width: 200px; height: 300px; background: red;"> 
    <div style="display: inline-block; max-width: 100%; max-height: 100%; background: blue;"> 
       <img src="http://placehold.it/200x800" style="width: auto; height: auto; max-width: inherit; max-height: inherit" /> 

    </div> 
</div> 

的結果是錯誤的,導致IMG被覆蓋在DIV。我希望img像第一個例子那樣調整大小。

Child with max-height: 100% overflows parent他們指出div需要有高度和寬度,但div不會填滿整個img。

實際上有一種方法可以做到嗎?

我的整個佈局更復雜,完全響應Flex上的和這裏的例子只是一個近似值集中在我有問題。因此,任何固定高度和寬度的解決方案都是錯誤的。

回答

3

不要使用額外的div。只需使用background-image即可。更清晰,更簡單,更多語義。可以將東西絕對放在一個包裝紙上div

CSS:

.wrapper { 
    width: 200px; 
    height: 300px; 
    background: url('http://placehold.it/200x800'), red; 
    background-size: auto 100%; 
    background-repeat: no-repeat; 
} 

HTML:

<div class="wrapper"></div> 

Demo

example

或者,如果你有一個額外的div是死心塌地,就可以完成相同的效果是這樣的:

CSS:

.wrapper { 
    width: 200px; 
    height: 300px; 
    background: red; 
} 

.inner-wrapper { 
    width: 0; 
    height: 100%; 
} 

.inner-wrapper img { 
    height: 100%; 
} 

HTML:

<div class="wrapper"> 
    <div class="inner-wrapper"> 
     <img src="http://placehold.it/200x800"> 
    </div> 
</div> 

Demo

example

+0

當然,我不希望圖像被剪切。我想把它作爲第一個例子 –

+0

@DidacMontero更新了我的答案:-) – bookcasey

+0

是的,你是對的,結果和他第一個例子一樣。但我需要一個div圍繞IMG填充完全相同的區域作爲IMG的原因,當我將絕對位置添加到div的孩子,它應該是絕對相對IMG –

0

試試這個,如果你想使你的形象響應

<div style="width:40%; height: 80%; background: red;"> 
       <img src="http://placehold.it/200x800"/> 

    </div> 

CSS

img{ 
    height:50%; 
    width:50%; 
} 

(如果你想爲一個特定的圖像THN您可以創建一個ID,並給這個屬性,並使用該ID爲圖像)

試試它小提琴。

0

我找到了一個解決方案,但不是純粹的CSS。我不得不使用JQuery,我對它並不高興,但它工作。

我很高興聽到一些純粹的CSS解決方案(具有響應式佈局和任何圖像大小),但到目前爲止,沒有提供適用於我的情況。

DEMO

HTML

<div class="container"> 
    <img src="//placehold.it/200x300" class="img-responsive centered" /> 
    <div class="img-wrapper centered"> 
     <div class="point" style="bottom: 0%; right: 0%;"></div> 
     <div class="point" style="top: 0%; right: 0%;"></div> 
     <div class="point" style="top: 0%; left: 0%;"></div> 
     <div class="point" style="bottom: 0%; left: 0%;"></div> 
    </div> 
</div> 

CSS

html, body{ height: 100%;} 
.container { 
    position: relative; 
    width: 100%; 
    height: 100%; 
    background: red; 
} 
.centered { 
    position:absolute; 
    max-width: 100%; 
    max-height: 100%; 
    right: 0%; 
    top: 50%; 
    transform: translateY(-50%); 
} 
.img-responsive { 
    position: absolute; 
    width: auto; 
    height: auto; 
} 
.img-wrapper { 
    max-height: 100%; 
    max-width: 100%; 
} 
.point { 
    background: green; 
    width: 6px; 
    height: 6px; 
    margin-left: -3px; 
    margin-top: -3px; 
    position: absolute; 
} 

的Javascript

//For better performance, use some of the plugins here: http://stackoverflow.com/questions/10086693/jquery-resize-on-div-element 
var img = $("img.img-responsive"); 
$(window).resize(function() { 
    var wrapperImg = img.parent().find(".img-wrapper"); 
    if (img.width() !== wrapperImg.width() && img.height() !== wrapperImg.height()) { 
     wrapperImg.width(img.width()); 
     wrapperImg.height(img.height()); 
    } 
}); 

//http://stackoverflow.com/questions/3588102/jquery-load-not-working-on-my-image#answer-3590761 
img.one('load', function() { 
    $(this).parent().find(".img-wrapper").css({ 
     "max-width": this.naturalWidth + "px", 
      "max-height": this.naturalHeight + "px", 
      "width": this.width + "px", 
      "height": this.height + "px" 
    }); 
}).each(function() { 
    if (this.complete) $(this).load(); 
});