Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML-CSS div is 3px taller than child

I'm working on a project that uses several divs of the same class, each containing a single child element that might be an image or an iframe, of unspecified height. I'd like the container div to be exactly the height of its child element, but the default height is 3px taller than the child.

I've got a JSfiddle demonstrating the problem at http://jsfiddle.net/52me041n/2/.

HTML:

<div class="outside">
    <img class="inside" id="pic" src="https://images.duckduckgo.com/iu/?u=http%3A%2F%2Fblogs.cisco.com%2Fwp-content%2Fuploads%2Fclouds.png&f=1" height="200px"/>
</div>
<br/>
<div class="outside">
    <iframe class="inside" width="420" height="315" src="//www.youtube.com/embed/VwTnyRHEZSQ" frameborder="0" allowfullscreen></iframe>
</div>

CSS:

.outside{
background-color: red;
}

I'd like to know whether it's possible to set the div to the proper height with just CSS, and if not, how to right it with JS.

like image 314
user1576628 Avatar asked Jan 08 '15 06:01

user1576628


People also ask

Can a child div be bigger than parent?

The child DIV needs to be the same width as the browser viewport. The child DIV must stay as a child of the parent div.

How do you make a div the same height as another?

Place both DIVs into a container DIV that's set to 100% of page height and set both interior DIVS to 100%. They will fill up the container DIV and the height of both will be equal.

How do I make a div the same height as a parent?

However if you do have given explicit height to the parent element, then you could just use height:100% on the child.

How do I make my div take 100% height of parent div?

Answer: Set the 100% height for parents too And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.


2 Answers

Updated the fiddle. http://jsfiddle.net/52me041n/3/

Use -

img, iframe {
    display: block;
}
like image 148
Sujit Agarwal Avatar answered Oct 21 '22 15:10

Sujit Agarwal


Images are not on the same baseline as text.

add

vertical-align:bottom;

to your img css

fiddle

like image 37
huan feng Avatar answered Oct 21 '22 16:10

huan feng