Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying elements in multiple rows using CSS
    primarykey
    data
    text
    <p>I have a fixed width container <code>&lt;div&gt;</code> that displays one or more widget <code>&lt;div&gt;</code>s. I want it to look like this:</p> <p><img src="https://i.stack.imgur.com/oWAJm.png" alt="rows in CSS"> &lt;- grey blocks are widgets, red border is the container</p> <p>Simplified, my structure in HTML looks like this:</p> <pre><code>&lt;div id="container"&gt; &lt;div id="widget1"&gt;1&lt;/div&gt; &lt;div id="widget2"&gt;2&lt;/div&gt; &lt;div id="widget3"&gt;3&lt;/div&gt; &lt;div id="widget4"&gt;4&lt;/div&gt; &lt;div id="widget5"&gt;5&lt;/div&gt; &lt;div id="widget6"&gt;6&lt;/div&gt; &lt;div id="widget7"&gt;7&lt;/div&gt; &lt;/div&gt; </code></pre> <p><strong>Considerations</strong></p> <ol> <li>Widgets will have a fixed height e.g. 100px</li> <li>Widgets will have a fixed width e.g. 100px but they may also be a multiple of that width (plus any margins crossed - see widget 1)</li> <li>Widgets should be spaced nicely with a margin (or similar) e.g. 10px</li> <li>I don't know how many widgets there will be (the user can assign as many or few as they like to the container).</li> <li>The container is a fixed width but doesn't have any "visual" styling (the red border is there for demonstration)</li> <li>Solution has to work in modern browsers (and MSIE7) and would ideally be pure CSS.</li> </ol> <p>Because of consideration <em>4.</em> I can't assign additional markup e.g. row div, classes (.first-child, .last-child) and because of <em>2.</em> <code>:nth-child</code> wouldn't work AFAIK.</p> <p><strong>Things I've tried</strong></p> <p>margin-left on widgets with <code>:first-child</code> setting <code>margin-left: 0</code> won't display a new row properly.</p> <p>margin-right on widgets with <code>:last-child</code> setting <code>margin-right: 0</code> the first row forces the container div wider and <a href="http://www.quirksmode.org/css/contents.html" rel="nofollow noreferrer">last-child isn't supported until MSIE9</a>.</p> <p>equal left and right margins (e.g. margin: 0 5px 10px) forces the container wide again.</p> <p>overflow - works great in my head! Not so much with either margins or padding.</p> <p>Is there a way to do this in CSS?</p> <p><a href="http://jsfiddle.net/agtb/VHXGT/" rel="nofollow noreferrer">http://jsfiddle.net/agtb/VHXGT/</a></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.
    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