Let\'s say I have this HTML:
Features
- Smells Good
- <
Maybe it's is completely possible with only CSS but I prefer to avoid "float" as much as I can because it interferes with it's parent's height.
If you are using jQuery, you can create a simple `wrapN` plugin that is similar to `wrapAll` except it only wraps "N" elements and then breaks and wraps the next "N" elements using a loop. Then set your wrappers class to `display: block;`.
(function ($) {
$.fn.wrapN = function (wrapper, n, start) {
if (wrapper === undefined || n === undefined) return false;
if (start === undefined) start = 0;
for (var i = start; i < $(this).size(); i += n)
$(this).slice(i, i + n).wrapAll(wrapper);
return this;
};
}(jQuery));
$(document).ready(function () {
$("li").wrapN("<span class='break' />", 3);
});
Here is a JSFiddle of the finished product:
http://jsfiddle.net/dustinpoissant/L79ahLoz/
When rewriting the html is allowed, you can nest <ul>
s within the <ul>
and just let the inner <li>
s display as inline-block. This would also semantically make sense IMHO, as the grouping also is reflected within the html.
<ul>
<li>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</li>
<li>
<ul>
<li>Item 4</li>
<li>Item 5</li>
<li>Item 6</li>
</ul>
</li>
</ul>
li li { display:inline-block; }
$(function() { $('img').attr('src', 'http://phrogz.net/tmp/alphaball.png'); });
h3 {
border-bottom: 1px solid #ccc;
font-family: sans-serif;
font-weight: bold;
}
ul {
margin: 0.5em auto;
list-style-type: none;
}
li li {
text-align: center;
display: inline-block;
padding: 0.1em 1em;
}
img {
width: 64px;
display: block;
margin: 0 auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h3>Features</h3>
<ul>
<li>
<ul>
<li><img />Smells Good</li>
<li><img />Tastes Great</li>
<li><img />Delicious</li>
</ul>
</li>
<li>
<ul>
<li><img />Wholesome</li>
<li><img />Eats Children</li>
<li><img />Yo' Mama</li>
</ul>
</li>
</ul>
I've been able to make it work on inline LI elements. Unfortunately, it does not work if the LI elements are inline-block:
Live demo: http://jsfiddle.net/dWkdp/
Or the cliff notes version:
li {
display: inline;
}
li:nth-child(3):after {
content: "\A";
white-space: pre;
}
I know you didn't want to use floats and the question was just theory but in case anyone finds this useful, here's a solution using floats.
Add a class of left to your li
elements that you want to float:
<li class="left"><img src="http://phrogz.net/tmp/alphaball.png">Smells Good</li>
and amend your CSS as follows:
li { text-align:center; float: left; clear: left; padding:0.1em 1em }
.left {float: left; clear: none;}
http://jsfiddle.net/chut319/xJ3pe/
You don't need to specify widths or inline-blocks and works as far back as IE6.
A better solution is to use -webkit-columns:2;
http://jsfiddle.net/YMN7U/889/
ul { margin:0.5em auto;
-webkit-columns:2;
}
An easy way to split lists into rows is by floating the individual list items and then the item that you want to go to the next line you can clear the float.
for example
<li style="float: left; display: inline-block"></li>
<li style="float: left; display: inline-block"></li>
<li style="float: left; display: inline-block"></li>
<li style="float: left; display: inline-block; clear: both"></li> --- this will start on a new line
<li style="float: left; display: inline-block"></li>
<li style="float: left; display: inline-block"></li>