Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ensure a DIV stretches to fit its contents?

Tags:

I have this HTML with a table:

<div
  class="currentDesignDiv"
  style="margin-bottom:5px;">
  <div>
    <img
      alt="Thumbnail"
      src="http://www.propertyware.com/templates/
        <bean:write name="currentTemplate" property="primaryKey"/>
      /thumb.gif"/>
  </div>
  <div>
    <table>
      <tr>
        <th>Your Current Design: </th>
        <td>Template #: 
          <bean:write name="currentTemplate" property="primaryKey"/>
        </td>
      </tr>
      <tr>
        <th>Last Updated: </th>
        <td>
          <bean:write name="currentTemplate" property="modifiedByAsString"/>
        </td>
      </tr>
      <tr>
        <th>Total Pages: </th>
        <td>
          <bean:write name="numberOfPages"/>
        </td>
      </tr>
    </table>
  </div>

Being styled by this CSS:

 .currentDesignDiv{
   background-color:#e7e7e7;
   border:1px solid #9c9c9c;
   padding:5px;
   width:100%;
   min-width:860px;
 }
 .currentDesignDiv div{
   float:left;
   width:33%;
 }
 .currentDesignDiv div table{
   font-size:9pt;
   text-align:left;
   margin:17px;
 }
 .currentDesignDiv div:first-child img{
   margin:17px;
   width:80px;
 }

It looks OK on my big monitor, but when I change to my small monitor the right-most div, which has an image inside of it, floats outside of the parent div. Does anybody have a solution for that?

like image 277
hdx Avatar asked Jul 21 '09 19:07

hdx


People also ask

How do I fit the content of a div?

Using inline-block property: Use display: inline-block property to set a div size according to its content.

How do you make a div fit the width of its contents?

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 stretch a div to fit a container?

So setting a height of zero on a display:table-row wrapper div made that row fit to the content image exactly. Setting a height of 100% on the content div made it nicely fill the space in between the image and the minimum height of the parent container. Voila! Works in Firefox 25, Chrome 32, Opera 18 and IE10.

How do I make the content fit the screen in HTML?

You should set body and html to position:fixed; , and then set right: , left: , top: , and bottom: to 0; . That way, even if content overflows it will not extend past the limits of the viewport.


2 Answers

set

overflow: auto; 

for the outer div. This should make your parent div expand even though its children have a float property. (IE has a few problems with this, but can be fixed by some padding)

like image 176
Virat Kadaru Avatar answered Sep 22 '22 20:09

Virat Kadaru


Perhaps try giving the outer div this css...

display: inline-block;

Since floated elements do not really have any layout of their own, the outer div just acts like an empty div, not stretching to fit the elements inside of it.

Making the outer div inline-block will essentially make the inner divs take up some actual space in the outer div.

like image 36
Sir David of Lee Avatar answered Sep 22 '22 20:09

Sir David of Lee