Reputation: 2086
I have a div with a defined width and height. Then I have an image which I want to fit on it (keeping proportions). Like that JsFiddle :
<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>
Now I want a div wrapped on the img (that is ajusted exactly to the img width and height and position) because then want to have some elements inside the div with position:absolute
relatively to the img. Check on 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>
The result is wrong, cause the img is overlayed on the div. I want the img to resize like in the first example.
In Child with max-height: 100% overflows parent they pointed out that the div needs to have a height and a width, but then the div will not be filling the whole img.
There is actually a way to do it?
My whole layout is more complex and totally responsive with flex on top and the example here is just an approximation focused on the issue I have. So any solution with fixed height and width will be wrong.
Upvotes: 1
Views: 3982
Reputation: 2086
I've found a solution but not pure CSS. I had to use JQuery and I'm not so happy about it but it works.
I would be glad to hear from some pure CSS solution (with a responsive layout and any image size), but up to now non of the offered worked for my case.
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();
});
Upvotes: 0
Reputation: 31
Try this if you are trying to make your image responsive
<div style="width:40%; height: 80%; background: red;">
<img src="http://placehold.it/200x800"/>
</div>
CSS
img{
height:50%;
width:50%;
}
(if you want it for a particular image thn you can create an ID and give this properties and use that ID for that image)
Try it on fiddle.
Upvotes: 0
Reputation: 40413
Don't use an extra div
. Just use a background-image
. Cleaner, easier, more semantic. Can position things absolutely on the one wrapper 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>
Alternatively, if you're dead set on having an extra div
, you can accomplish the same effect like this:
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>
Upvotes: 4