title: How do I make a div automatically wide enough to accommodate its floating children?
body: <p>How do I make a div automatically wide enough to accomodate its floating children?</p>
  
  <pre><code>&lt;div class="Parent"&gt; &lt;!-- 3 Children, should be 450px wide--&gt;
    &lt;div class="Child"&gt;&lt;/div&gt;
    &lt;div class="Child"&gt;&lt;/div&gt;
    &lt;div class="Child"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="Parent"&gt; &lt;!-- 2 children, should be 300px wide--&gt;
    &lt;div class="Child"&gt;&lt;/div&gt;
    &lt;div class="Child"&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The Child divs are all "float:left" with fixed widths. How do I make the Parent div be equal to the width of it's floating children? The number of children in each parent varies and is unknown but the children must line up in a row, without wrapping to a new line.</p>
  
  <p>Example: If all three children are 150px, the parent should become 450px so that none of the children wrap. If there are two 150px children, the parent should become 300px.</p>
  
  <p>Here's a demo of the problem:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#MainNavBar {
      background:url('../assets/design/images/MainNavGrad.jpg') repeat-x scroll 0 0 transparent;
      height:43px;
      margin-bottom:10px;
  }
  
  
  .NavSpace {
     background:url('../assets/design/images/NavSpacer.jpg');
     float:left;
     width:2px;
     height:100%;
  }
  
  
  
  /*------------------------------------*\
     NAV
  \*------------------------------------*/
  #nav{
     float:left;
     width:100%;
     list-style:none;
     font-weight:bold;
     margin-bottom:10px;
  }
  #nav li{
     float:left;
     margin-right:10px;
     position:relative;
     display:block;
  }
  
  a.MainNavButton{ /*Main Buttons */
     text-transform:uppercase;
     font-family:verdana;
     font-weight:bold;
     font-size:11px;
     text-align:center;
     text-shadow:1px 1px 1px rgba(0,0,0,0.75); /* Text shadow to lift it a little */
     color:#fff;  
     display:block;
     padding:12px 21px 16px 21px ; /* TOP LEFT Bottom RIGHT */     
     -moz-border-radius:2px;
     -webkit-border-radius:2px;
     border-radius:2px;
  }
  
  #nav li a:hover{
     color:#fff;
     background:#444444;  /* Solid colour fall-back */
     text-decoration:underline;
  }
  
  /*--- DROPDOWN ---*/
  
  /*
   * &lt;div class="SubNavWrapper"&gt;
     &lt;div class="SubNav"&gt;
        &lt;div class="SubNavSectionWrapper"&gt;
           &lt;h5&gt;List 1&lt;/h5&gt;
           &lt;ul class="SubNavSectionList"&gt;
              &lt;li&gt;&lt;a href="#"&gt;The product&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#"&gt;Meet the team&lt;/a&gt;&lt;/li&gt;
           &lt;/ul&gt;
        &lt;/div&gt;
        &lt;div class="SubNavSectionWrapper"&gt;
           &lt;h5&gt;List 2&lt;/h5&gt;
           &lt;ul class="SubNavSectionList"&gt;
              &lt;li&gt;&lt;a href="#"&gt;The product&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#"&gt;Meet the team&lt;/a&gt;&lt;/li&gt;
           &lt;/ul&gt;
        &lt;/div&gt;
     &lt;/div&gt;
  &lt;/div&gt;&lt;!-- END Nav Pad--&gt;
  */
  
  
  div.SubNavWrapper {   
     float:left;
     padding:30px;
     padding-top:0px;
     position:absolute;
     background:transparent;
     left:-9999px; /* Hide off-screen when not needed (this is more accessible than display:none;) */
  }
  
  div.SubNav {
     float:left;
     clear:both;
     padding:30px;
     border:1px solid #222;
     border-top:none;
     background:#444;
  }
  
  div.SubNavSectionWrapper {   
     float:left;
     width:150px;
     height:200px;
     margin-right:45px;
  }
  
  div.SubNavSectionTitle { /* Title of a sub-section */
  text-transform:uppercase;
  font-family:verdana;
  font-weight:bold;
  font-size:11px;
  padding-bottom:3px;
  margin-bottom:3px;
  border-bottom:1px solid #333;
  color:#fff;  
  }
  
  ul.SubNavSectionList {
     list-style:none;
  }
  
  ul.SubNavSectionList li{
     padding-top:1px; /* Introducing a padding between the li and the a give the illusion spaced items */
  }
  
  ul.SubNavSectionList li a { /* Sub Menu Links */
  font-family:verdana;
  font-weight:bold;
  font-size:11px;
  color:#fff;
  padding:4px 0px;
  }
  
  ul.SubNavSectionList a{
     white-space:nowrap; /* Stop text wrapping and creating multi-line dropdown items */
     display:block;
  }
  
  #nav li:hover div.SubNavWrapper{ /* Display the dropdown on hover */
     left:-30px; /* Bring back on-screen when needed */ /* must match SubNavWrapper!! */
     opacity:1; /* Fade to opaque */
  }
  
  #nav li:hover a{ /* Set styles for top level when dropdown is hovered */
     background:#444444; /* Solid colour fall-back */
     text-decoration:underline;
  }
  
  #nav li:hover ul.SubNavSectionList a{ /* Override some top level styles when dropdown is hovered */
     text-decoration:none;
  }
  
  #nav li:hover ul.SubNavSectionList li a:hover{ /* Set styles for dropdown when items are hovered */
     background:#333; /* Solid colour fall-back */
     text-decoration:underline;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="MainNavBar"&gt;         
  &lt;ul id="nav"&gt;
      &lt;li&gt;
         &lt;a class="MainNavButton" href="#"&gt;Home&lt;/a&gt;
      &lt;/li&gt;    
      &lt;li&gt;&lt;!-- Animals Menu --&gt;
         &lt;a class="MainNavButton" href="#"&gt;Animals&lt;/a&gt;&lt;!-- hover over me to show the "animals" menu --&gt;
         &lt;div class="SubNavWrapper"&gt; &lt;!-- This is a wrapper that is absolutly positioned off the screen, until
                                               the "animals" link is hovered over --&gt;                  
            &lt;div class="SubNav"&gt;&lt;!-- This is a parent with floating children --&gt;
                &lt;!-- this is a child that floats left --&gt;
                &lt;div class="SubNavSectionWrapper"&gt;                        
                  &lt;div class="SubNavSectionTitle"&gt;Fish&lt;/div&gt;
                  &lt;ul class="SubNavSectionList"&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Gold Fish&lt;/a&gt;&lt;/li&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Barracuda&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
               &lt;/div&gt;
               &lt;!-- this is a child that floats left --&gt;
               &lt;div class="SubNavSectionWrapper"&gt;
                  &lt;div class="SubNavSectionTitle"&gt;Reptiles&lt;/div&gt;
                  &lt;ul class="SubNavSectionList"&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Lizzards&lt;/a&gt;&lt;/li&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Snakes&lt;/a&gt;&lt;/li&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Iguanas&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
               &lt;/div&gt;
               
               &lt;!-- this is a child that floats left --&gt;
               &lt;div class="SubNavSectionWrapper"&gt;
                  &lt;div class="SubNavSectionTitle"&gt;Birds&lt;/div&gt;
                  &lt;ul class="SubNavSectionList"&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Parakeets&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
               &lt;/div&gt;              
            &lt;/div&gt;&lt;!-- END SubNav --&gt;
         &lt;/div&gt;&lt;!-- END SubNavWrapper --&gt;
      &lt;/li&gt;&lt;!-- END Animals Menu --&gt;
  &lt;/ul&gt;  
  &lt;/div&gt; &lt;!-- END Nav Bar --&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>When you hover over animals, you can see that birds, reptiles and fish are stacked on top of each other. They should show side by side.</p>
  
  <p>This is what I want it to look like:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="true" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>#MainNavBar {
     background:url('../assets/design/images/MainNavGrad.jpg') repeat-x scroll 0 0 transparent;
     height:43px;
     margin-bottom:10px;
  }
  
  
  .NavSpace {
     background:url('../assets/design/images/NavSpacer.jpg');
     float:left;
     width:2px;
     height:100%;
  }
  
  
  
  /*------------------------------------*\
     NAV
  \*------------------------------------*/
  #nav{
     float:left;
     width:100%;
     list-style:none;
     font-weight:bold;
     margin-bottom:10px;
  }
  #nav li{
     float:left;
     margin-right:10px;
     position:relative;
     display:block;
  }
  
  a.MainNavButton{ /*Main Buttons */
     text-transform:uppercase;
     font-family:verdana;
     font-weight:bold;
     font-size:11px;
     text-align:center;
     text-shadow:1px 1px 1px rgba(0,0,0,0.75); /* Text shadow to lift it a little */
     color:#fff;  
     display:block;
     padding:12px 21px 16px 21px ; /* TOP LEFT Bottom RIGHT */     
     -moz-border-radius:2px;
     -webkit-border-radius:2px;
     border-radius:2px;
  }
  
  #nav li a:hover{
     color:#fff;
     background:#444444;  /* Solid colour fall-back */
     text-decoration:underline;
  }
  
  /*--- DROPDOWN ---*/
  
  /*
   * &lt;div class="SubNavWrapper"&gt;
     &lt;div class="SubNav"&gt;
        &lt;div class="SubNavSectionWrapper"&gt;
           &lt;h5&gt;List 1&lt;/h5&gt;
           &lt;ul class="SubNavSectionList"&gt;
              &lt;li&gt;&lt;a href="#"&gt;The product&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#"&gt;Meet the team&lt;/a&gt;&lt;/li&gt;
           &lt;/ul&gt;
        &lt;/div&gt;
        &lt;div class="SubNavSectionWrapper"&gt;
           &lt;h5&gt;List 2&lt;/h5&gt;
           &lt;ul class="SubNavSectionList"&gt;
              &lt;li&gt;&lt;a href="#"&gt;The product&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#"&gt;Meet the team&lt;/a&gt;&lt;/li&gt;
           &lt;/ul&gt;
        &lt;/div&gt;
     &lt;/div&gt;
  &lt;/div&gt;&lt;!-- END Nav Pad--&gt;
  */
  
  
  div.SubNavWrapper {   
     float:left;
     padding:30px;
     padding-top:0px;
     position:absolute;
     background:transparent;
     left:-9999px; /* Hide off-screen when not needed (this is more accessible than display:none;) */
  }
  
  div.SubNav {
     float:left;
     clear:both;
     width:600px;
     padding:30px;
     border:1px solid #222;
     border-top:none;
     background:#444;
  }
  
  div.SubNavSectionWrapper {   
     float:left;
     width:150px;
     height:200px;
     margin-right:45px;
  }
  
  div.SubNavSectionTitle { /* Title of a sub-section */
  text-transform:uppercase;
  font-family:verdana;
  font-weight:bold;
  font-size:11px;
  padding-bottom:3px;
  margin-bottom:3px;
  border-bottom:1px solid #333;
  color:#fff;  
  }
  
  ul.SubNavSectionList {
     list-style:none;
  }
  
  ul.SubNavSectionList li{
     padding-top:1px; /* Introducing a padding between the li and the a give the illusion spaced items */
  }
  
  ul.SubNavSectionList li a { /* Sub Menu Links */
  font-family:verdana;
  font-weight:bold;
  font-size:11px;
  color:#fff;
  padding:4px 0px;
  }
  
  ul.SubNavSectionList a{
     white-space:nowrap; /* Stop text wrapping and creating multi-line dropdown items */
     display:block;
  }
  
  #nav li:hover div.SubNavWrapper{ /* Display the dropdown on hover */
     left:-30px; /* Bring back on-screen when needed */ /* must match SubNavWrapper!! */
     opacity:1; /* Fade to opaque */
  }
  
  #nav li:hover a{ /* Set styles for top level when dropdown is hovered */
     background:#444444; /* Solid colour fall-back */
     text-decoration:underline;
  }
  
  #nav li:hover ul.SubNavSectionList a{ /* Override some top level styles when dropdown is hovered */
     text-decoration:none;
  }
  
  #nav li:hover ul.SubNavSectionList li a:hover{ /* Set styles for dropdown when items are hovered */
     background:#333; /* Solid colour fall-back */
     text-decoration:underline;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id="MainNavBar"&gt;         
  &lt;ul id="nav"&gt;
      &lt;li&gt;
         &lt;a class="MainNavButton" href="#"&gt;Home&lt;/a&gt;
      &lt;/li&gt;    
      &lt;li&gt;&lt;!-- Animals Menu --&gt;
         &lt;a class="MainNavButton" href="#"&gt;Animals&lt;/a&gt;&lt;!-- hover over me to show the "animals" menu --&gt;
         &lt;div class="SubNavWrapper"&gt; &lt;!-- This is a wrapper that is absolutly positioned off the screen, until
                                               the "animals" link is hovered over --&gt;                  
            &lt;div class="SubNav"&gt;&lt;!-- This is a parent with floating children --&gt;
                &lt;!-- this is a child that floats left --&gt;
                &lt;div class="SubNavSectionWrapper"&gt;                        
                  &lt;div class="SubNavSectionTitle"&gt;Fish&lt;/div&gt;
                  &lt;ul class="SubNavSectionList"&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Gold Fish&lt;/a&gt;&lt;/li&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Barracuda&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
               &lt;/div&gt;
               &lt;!-- this is a child that floats left --&gt;
               &lt;div class="SubNavSectionWrapper"&gt;
                  &lt;div class="SubNavSectionTitle"&gt;Reptiles&lt;/div&gt;
                  &lt;ul class="SubNavSectionList"&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Lizzards&lt;/a&gt;&lt;/li&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Snakes&lt;/a&gt;&lt;/li&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Iguanas&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
               &lt;/div&gt;
               
               &lt;!-- this is a child that floats left --&gt;
               &lt;div class="SubNavSectionWrapper"&gt;
                  &lt;div class="SubNavSectionTitle"&gt;Birds&lt;/div&gt;
                  &lt;ul class="SubNavSectionList"&gt;
                     &lt;li&gt;&lt;a href="#"&gt;Parakeets&lt;/a&gt;&lt;/li&gt;
                  &lt;/ul&gt;
               &lt;/div&gt;              
            &lt;/div&gt;&lt;!-- END SubNav --&gt;
         &lt;/div&gt;&lt;!-- END SubNavWrapper --&gt;
      &lt;/li&gt;&lt;!-- END Animals Menu --&gt;
  &lt;/ul&gt;  
  &lt;/div&gt; &lt;!-- END Nav Bar --&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I got it by forcing the parent to be a fixed width. But the parent needs to vary based on it's children and not be a fixed width.</p>