How to display a list in horizontal groups of li elements?

Brian J

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>
Paulie_D

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set horizontal to list vertical li ul

From Dev

How do wrap these groups of li elements in ul containers?

From Dev

How to vertically align elements in horizontal list

From Dev

inline-block will not change elements in my unordered list to horizontal display

From Dev

inline-block will not change elements in my unordered list to horizontal display

From Dev

how to average groups of elements in a list in R?

From Dev

How to create a list of the autocomplete <li> elements when parent <ul> element contains the attribute style="display: none;" through Java Selenium

From Dev

Display li horizontally with wide li elements

From Dev

How to get elements from a list, by groups of 10? (or 5, or 3, etc)

From Dev

How to find the minimum number of groups needed for grouping a list of elements?

From Dev

How to display the elements of a returned list in Roxygen?

From Dev

HTML how to display elements in a list on a new line

From Dev

How to display elements in a Linked list by recursion?

From Dev

How to Display Horizontal Button

From Dev

Display list items horizontal in a menu

From Dev

How to get a list of the <li> elements in an <ul> with Selenium using Python?

From Dev

How to float li elements inside multi column list

From Dev

How can I group my array into groups of 3 elements, sort them by the last element and display the elements?

From Dev

How to determine the display order of <li> in a list with 4 columns?

From Dev

Change class of list of li elements

From Dev

Why elements don't display horizontal?

From Dev

How to avoid horizontal elements being displaced within a navbar when the display width is small?

From Dev

How to display horizontal fraction symbol

From Dev

How to display in horizontal scroll view

From Dev

How to style two groups of <li> in a single line?

From Dev

How to wrap groups of li tags inside ul

From Dev

How to show and hide list items(li) elements inside unordered list(ul) using jquery(preferably)?

From Dev

How to display my list<T> elements in a text box

From Dev

Select elements in list in groups of 2 - Bash

Related Related

  1. 1

    How to set horizontal to list vertical li ul

  2. 2

    How do wrap these groups of li elements in ul containers?

  3. 3

    How to vertically align elements in horizontal list

  4. 4

    inline-block will not change elements in my unordered list to horizontal display

  5. 5

    inline-block will not change elements in my unordered list to horizontal display

  6. 6

    how to average groups of elements in a list in R?

  7. 7

    How to create a list of the autocomplete <li> elements when parent <ul> element contains the attribute style="display: none;" through Java Selenium

  8. 8

    Display li horizontally with wide li elements

  9. 9

    How to get elements from a list, by groups of 10? (or 5, or 3, etc)

  10. 10

    How to find the minimum number of groups needed for grouping a list of elements?

  11. 11

    How to display the elements of a returned list in Roxygen?

  12. 12

    HTML how to display elements in a list on a new line

  13. 13

    How to display elements in a Linked list by recursion?

  14. 14

    How to Display Horizontal Button

  15. 15

    Display list items horizontal in a menu

  16. 16

    How to get a list of the <li> elements in an <ul> with Selenium using Python?

  17. 17

    How to float li elements inside multi column list

  18. 18

    How can I group my array into groups of 3 elements, sort them by the last element and display the elements?

  19. 19

    How to determine the display order of <li> in a list with 4 columns?

  20. 20

    Change class of list of li elements

  21. 21

    Why elements don't display horizontal?

  22. 22

    How to avoid horizontal elements being displaced within a navbar when the display width is small?

  23. 23

    How to display horizontal fraction symbol

  24. 24

    How to display in horizontal scroll view

  25. 25

    How to style two groups of <li> in a single line?

  26. 26

    How to wrap groups of li tags inside ul

  27. 27

    How to show and hide list items(li) elements inside unordered list(ul) using jquery(preferably)?

  28. 28

    How to display my list<T> elements in a text box

  29. 29

    Select elements in list in groups of 2 - Bash

HotTag

Archive