Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I set this div to be the minimum possible width to display its floating contents?
    primarykey
    data
    text
    <h2>Problem</h2> <p>I have "boxes" that float left so that I can display them in a line until they need to wrap. This works well, but my coloured background doesn't shrink to the minimum, it expands to the maximum.</p> <h2>JSFiddle</h2> <p><a href="http://jsfiddle.net/RLRh6/" rel="noreferrer">http://jsfiddle.net/RLRh6/</a></p> <p>(Expand and shrink the <em>Result</em> section to see the effect)</p> <p><strong>HTML</strong></p> <pre><code>&lt;div class="container"&gt; &lt;div class="boxes"&gt; &lt;div class="box"&gt;&lt;/div&gt; &lt;div class="box"&gt;&lt;/div&gt; &lt;div class="box"&gt;&lt;/div&gt; &lt;div class="box"&gt;&lt;/div&gt; &lt;div class="box"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>CSS</strong></p> <pre><code>.container { background: #fcc; margin: 0 auto; max-width: 300px; } .boxes { background: #cfc; overflow: hidden; } .box { border: 1px dashed blue; width: 70px; height: 50px; float: left; margin: 2px; } </code></pre> <h2>What I Get</h2> <p>Note the extra green colour, right of the boxes:</p> <p><strong>Example 1</strong></p> <p><img src="https://i.stack.imgur.com/dqKHZ.png" alt="Example 1"></p> <p><strong>Example 2</strong></p> <p><img src="https://i.stack.imgur.com/vjhjn.png" alt="Example 2"></p> <h2>What I Want</h2> <p><strong>Example 1</strong></p> <p><img src="https://i.stack.imgur.com/kzGCf.png" alt="Example 1"></p> <p><strong>Example 2</strong></p> <p><img src="https://i.stack.imgur.com/nMoOd.png" alt="Example 2"></p> <h2>Question</h2> <p>Is it possible to have the green-background div ("<strong>.boxes</strong>") shrink to the minimum possible size to display the boxes without Javascript? <em>You should be able to shrink and expand the div freely and not see any green to the right of the boxes</em>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload