Usually I use an element of height 0 that clears as the last child in the container. This causes the container to βstretchβ around floating objects:
<div style="clear: both; line-height: 0; height: 0;"> </div>
This is an option in the QuirksMode article and has good browser compatibility.
I rewrote your code to include it and demonstrate the results:
<html> <head> <title>test</title> <style type="text/css"> div.text-field { border: 1px solid red; font-family: sans-serif; margin: 3px; float: left; } div.text-holder { border: 1px solid blue; } </style> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#output1").text($("#someid1 .text-holder").height()); $("#output2").text($("#someid2 .text-holder").height()); }); </script> </head> <body> <div id="someid1"> <div class="text-holder"> <div class="text-field">text here</div> </div> </div> <br> <div id="output1"> </div> <br><br><br> <div id="someid2"> <div class="text-holder"> <div class="text-field">text here</div> <div style="clear: both; line-height: 0; height: 0;"> </div> </div> </div> <br> <div id="output2"> </div> </body> </html>
The demo can also be viewed on the JSFiddle .
Jordan mack
source share