Guys I have a div like this:
<div id="image-container">
<img id="image" src="#" > //with an image inside
</div>
And styled this way:
#image-container {
width: 750px;
height: 360px !important;
text-align: center;
}
But when i load the page, and inspect element on the image to know its dimensions, this is what it says:
element.style {
height: 600px;
width: 600px;
}
Why doesnt the image inherit the width of the div? And I cant manually put the desired width of the image for some reason, so I cant do this:
#image {
width : 330px; //manually putting width
height: 303px; //same same which i cannot do this for some reason
}
Any idea why or how to solve this?
I all tried the solution below:
and here is what i get from inspect element:
#image {
height: inherit; // crossed-out
width: inherit; // crossed-out
}
Something is overwriting my image dimensions.
We have to give image tag width and height as 100%. And add the object-fit property value as contain. But the difference is if parent div size is more than image size then image will be automatically resized to parent div element size.
Answer: Use the CSS max-width Property Additionally, you can also apply the max-height property if you've a fixed height div element, so that the image doesn't overflow from the div's boundary horizontally or vertically.
If you want the parent dive to get the children height you need to give to the parent div a css property overflow:hidden; But to solve your problem you can use display: table-cell; instead of float... it will automatically scale the div height to its parent height... Save this answer.
width:inherit inherits width that defined by parent. This makes child width 25%, but if I redefine it with width:100% it will define width of child 50%.
You should set your image's width
and height
properties to either 100%
, inherit
or the same values so it completely fills the space of the parent.
#Container {
width: 200px;
height: 200px;
}
#Image {
width: inherit;
height: inherit;
}
You can check this on this fiddle. Also, make shure no other CSS is being loaded on the document.
Try this FIDDLE
#image-container {
width: 750px;
height: 360px !important;
text-align: center;
overflow: hidden;
}
#image-container img{
width: inherit;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With