2

I have a very long list of li elements greater than 30 items. When I display this in a ul menu dropdown the list stretches down vertically which requires the browser window to be scrolled down to view every list element.

I'd like to do is group each row in the list to display 3 li elements instead of one per row in the ul menu.

what I did try is setting the style .sub-menu li used by the menu to display: inline as suggested here to display the li elements inline but that made no difference to the ordering horizontally.

So instead of this:

  • Jim
  • Jane
  • Jack
  • Jones
  • Jackson
  • Jhonny

It would display like this (where each three li elements in the list is grouped horizontally):

Jane John Jared

Jim James Jones

Question:

How can you display a list in horizontal groupings of n li elements?

In my current setup I use this CSS for the menu:

nav li:hover .sub-menu {
    z-index: 2;
    opacity: 1;
    min-width: 100px;
}

.sub-menu {
    width: 100%;
    padding: 0px 0px;
    position: absolute;
    top: 100%;
    left: 0px;
    z-index: -1;
    opacity: 0;
    overflow: hidden;
    transition: opacity linear 0.15s;
    box-shadow: 0px 2px 3px rgba(0,0,0,0.2);
    background: #425563;
    text-align: center;
    display: inline-block;
}

    .sub-menu li {
        display: inline-block;
        font-size: 15px;
        float: left;
        margin-top: 0px;
    }

        .sub-menu li a {
            padding: 10px 5px;
            display: block;
            text-decoration: none;
            text-align: left;
            z-index: 3;
        }

            .sub-menu li a:hover, .sub-menu .current-item a {
                background: #01a982;
                -moz-box-shadow: 0 0 5px 0px #888;
                -webkit-box-shadow: 0 0 5px 0px #888;
                box-shadow: 0 0 5px 0px #888;
                color: #000;
            }


            .sub-menu li a input {
              display: inline-block;

            }

And this is the ul list which uses the sub-menu css style:

                <ul class="sub-menu" id="assetNameMenu">
                   @* li elements created in script *@
                </ul>
Brian Var
  • 6,029
  • 25
  • 114
  • 212

2 Answers2

6

Floats can do this quite simply.

You just clear the float after every this li

ul {
  list-style-type: none;
}
li {
  float: left;
  padding: 0 1em;
}
li:nth-child(3n+1) {
  clear: both;
}
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
  <li>Item 7</li>
  <li>Item 8</li>
  <li>Item 9</li>
</ul>

For inline-block there is an similar option as detailed in the answer to this question by @phrogz

ul {
  list-style-type: none;
}
li {
  display: inline;
}
li:nth-child(3n)::after {
  content: "\A";
 white-space: pre;
  ;
}
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
  <li>Item 7</li>
  <li>Item 8</li>
  <li>Item 9</li>
</ul>
Community
  • 1
  • 1
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
  • I tried that edit on the li style of sub-menu but the list is still the same, didn't group into three's. Any ideas why that is? This is the updated css http://hastebin.com/ijikonawah.css and this is a screenshot of the element in browser..http://picpaste.com/pics/menu-1M9luEus.1471444731.PNG – Brian Var Aug 17 '16 at 14:39
  • No idea but I can't tell much from images of code. You'd have to make a proper demo. – Paulie_D Aug 17 '16 at 14:41
  • I tested again by commenting out the `.sub-menu` attributes one by one. Commenting out `position: absolute;` renders the list items correctly in groups of three when hovering over the list. But removing this position option causes the other menu elements to be spaced far apart and not inline http://picpaste.com/menu-9HrKc4NS.PNG. Where as leaving this position property in shows the menu correctly inline but the li elements aren't grouped when hovered over http://picpaste.com/pics/menu-9QZGmRkZ.1471447583.PNG – Brian Var Aug 17 '16 at 15:26
  • Again, I can't diagnose code in images. I'd suggest you ask a NEW question with a more complete demo. – Paulie_D Aug 17 '16 at 15:28
  • Fair enough, increasing the width of the ul parent allowed the li elements to expand out. so setting `.sub-menu li:nth-child(3n+1) { clear: both; }` in addition to `.sub-menu { width: 500px; padding: 0px 0px; position: absolute; top: 100%; left: 0px; z-index: -1; opacity: 0; overflow: hidden; transition: opacity linear 0.15s; box-shadow: 0px 2px 3px rgba(0,0,0,0.2); background: #425563; text-align: center; list-style-type: none; float: left; }` – Brian Var Aug 17 '16 at 15:38
2

As an alternative, if you don't mind using newer CSS, you could do this with flexbox, too:

ul {
  display:         flex;
  list-style-type: none;
  width:           300px;
  flex-wrap:       wrap;
}

li {
  flex: 33%;
}
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
  <li>Item 7</li>
  <li>Item 8</li>
  <li>Item 9</li>
</ul>
Toby Deshane
  • 608
  • 1
  • 9
  • 23
  • (Whoever downvoted -- I'm fine with being wrong here, but please tell me what I overlooked.) – Toby Deshane Aug 17 '16 at 14:26
  • I can only think that your answer requires all the `li` all be the same width which may not be required or preferred. – Paulie_D Aug 17 '16 at 14:29
  • 2
    Fair enough. I just assumed they'd want neat columns in that situation. Good to have options, at least. :) – Toby Deshane Aug 17 '16 at 14:32
  • True but you wouldn't need flexbox to do that. Floats or inline block set to 33.3% would do the same thing. – Paulie_D Aug 17 '16 at 14:39
  • 1
    I can't think of a scenario where a person need not need neat columns. And we should use flexbox since it have a nicer support now. – Asim K T Nov 09 '17 at 12:02