Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my image width inherit from parent div width

Tags:

html

css

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.

like image 333
Belmark Caday Avatar asked Jun 04 '14 06:06

Belmark Caday


People also ask

How do I make an image fit in a parent div?

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.

How do you make an image take the full width of a div?

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.

How div inherit parent height and width?

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.

Is width inherited in CSS?

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%.


2 Answers

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.

like image 97
arielnmz Avatar answered Sep 24 '22 16:09

arielnmz


Try this FIDDLE

#image-container {
    width: 750px;
    height: 360px !important;
    text-align: center;
    overflow: hidden;
}

#image-container img{ 
    width: inherit;
}
like image 32
Raman Mandal Avatar answered Sep 24 '22 16:09

Raman Mandal