Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div has no height even if it has content [duplicate]

Tags:

html

css

I have a <div> which has some <img> in it. Every <img> is inside it's own <div>. The problem is that the outer div is not automatically taking the height of the content even though I set it's height to auto. Also to display the inner divs inline I set them to float: left. But if i remove the float the outer div behaves normally and takes the height of the content. But I need the imgs to be inline. Can anyone help me?

JSFiddle

HTML:

<div id="gallery">     <div class="gal-foto">         <img src="http://farm3.staticflickr.com/2819/10183644713_c1f49eb81f_b.jpg" class="gal-img">     </div>     <div class="gal-foto">         <img src="http://farm4.staticflickr.com/3694/10183642403_0c26d59769_b.jpg" class="gal-img">     </div>     <div class="gal-foto">         <img src="http://farm4.staticflickr.com/3764/10183532675_0ce4a0e877_b.jpg" class="gal-img">     </div>     <div class="gal-foto">         <img src="http://farm6.staticflickr.com/5331/10183598286_9ab37e273c_b.jpg" class="gal-img">     </div>     <div class="gal-foto">         <img src="http://farm6.staticflickr.com/5334/10183535585_04b18fa7da_b.jpg" class="gal-img">     </div> </div> 

CSS:

#gallery {     border: 1px solid;     border-radius: 10px 10px 10px 10px;     box-shadow: 0 0 15px rgba(50, 50, 50, 0.7) inset;     height: auto;     margin-top: 20px;     padding: 15px; } .gal-foto {     float: left;     margin: 3px;     position: relative; } .gal-img {     display: block;     max-height: 150px;     max-width: 150px; } 
like image 251
Beniamin Szabo Avatar asked Oct 14 '13 06:10

Beniamin Szabo


People also ask

Why does my div have no height?

The reason why the height or the containers is 0 is because you are floating all the content inside them and floated elements don't expand the height (or width) of their parents. As you are floating all elents in row it has 0 height and therfore so has .

How do you make div height not expand with content?

Answer: Use the CSS display Property You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).

How do I keep my DIVs the same height?

You can use Jquery's Equal Heights Plugin to accomplish, this plugins makes all the div of exact same height as other. If one of them grows and other will also grow. Usage: $(object). equalHeights([minHeight], [maxHeight]); Example 1: $(".


2 Answers

See the Demo here . Just add overflow: auto; to your main container.

#gallery {     border: 1px solid;     border-radius: 10px 10px 10px 10px;     box-shadow: 0 0 15px rgba(50, 50, 50, 0.7) inset;     height: auto;     margin-top: 20px;     padding: 15px;     overflow: auto; } 
like image 103
Vikas Ghodke Avatar answered Sep 30 '22 18:09

Vikas Ghodke


http://jsfiddle.net/WVL9a/

Add the following:

CSS:

.clearer { clear: both; } 

HTML:

<div id="gallery">     ....     <div class="clearer"></div> </div> 
like image 37
Ben Barkay Avatar answered Sep 30 '22 17:09

Ben Barkay