简体   繁体   中英

Height of a DIV-container

I've got a problem with DIV-containers.

 .inner-teaser { width: 100%; max-width: 1000px; margin: 0 auto; padding: 20px; } .bg-blue { background: blue; } .teaser-image { background-position: center; background-size: 350px; width: 250px; height: 250px; border-radius: 150px; } .image-left { float: left; margin-right: 50px; } 
 <div class="outer-teaser bg-blue"> <div class="inner-teaser"> <div class="teaser-image image-left" style="background-image: url(http://lorempixel.com/output/abstract-qc-640-480-5.jpg);">Image</div> <div class="teaser-text">Lorem ipsum dolor...</div> </div> </div> 

The inner-teaser should have the height from the teaser-image (250px) + 20px padding. But inner-teaser has only a height of 40px (2 * 20px padding).

Add overflow: hidden to .inner-teaser to force it to take its floating-children height:

 .inner-teaser { width: 100%; max-width: 1000px; margin: 0 auto; padding: 20px; overflow: hidden; } .bg-blue { background: blue; } .teaser-image { background-position: center; background-size: 350px; width: 250px; height: 250px; border-radius: 150px; } .image-left { float: left; margin-right: 50px; } 
 <div class="outer-teaser bg-blue"> <div class="inner-teaser"> <div class="teaser-image image-left" style="background-image: url(http://lorempixel.com/output/abstract-qc-640-480-5.jpg);">Image</div> <div class="teaser-text">Lorem ipsum dolor...</div> </div> </div> 

You can also use { display: inline-block } for .inner-teaser div.

 .inner-teaser { width: 100%; max-width: 1000px; margin: 0 auto; padding: 20px; display: inline-block; } .bg-blue { background: blue; } .teaser-image { background-position: center; background-size: 350px; width: 250px; height: 250px; border-radius: 150px; } .image-left { float: left; margin-right: 50px; } 
 <div class="outer-teaser bg-blue"> <div class="inner-teaser"> <div class="teaser-image image-left" style="background-image: url(http://lorempixel.com/output/abstract-qc-640-480-5.jpg);">Image</div> <div class="teaser-text">Lorem ipsum dolor...</div> </div> </div> 

just add after class class="teaser-text" see demo : demo

 .inner-teaser { width: 100%; max-width: 1000px; margin: 0 auto; padding: 20px; } .bg-blue { background: blue; } .teaser-image { background-position: center; background-size: 350px; width: 250px; height: 250px; border-radius: 150px; } .image-left { float: left; margin-right: 50px; } 
 <div class="outer-teaser bg-blue"> <div class="inner-teaser"> <div class="teaser-image image-left" style="background-image: url(http://lorempixel.com/output/abstract-qc-640-480-5.jpg);">Image</div> <div class="teaser-text">Lorem ipsum dolor...</div> <div style="clear:both"></div> </div> </div> 

v style="clear:both">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM