Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8852943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:31:36+00:00 2026-06-14T13:31:36+00:00

I want to format my unordered list into two sections of text. I’ve been

  • 0

I want to format my unordered list into two sections of text. I’ve been trying to find out how to do this with Google. but everyone seems interested in making their lists flow left to right, which is not what I want to do. What I’m looking for is a way to make the following:

 Food                Calories

 Fruit
    -Apple            90
    -Grape            5
    -Berries          
       -Strawberry    16
       -Blueberry     9
 Vegetable
    -Cucumber         12
    -Onions            
       -Red           29
       -White         34
       -Vidalia       47

Is this possible with an unordered list and CSS? I’d prefer not to use a table for this, since I’d like to make the <li> expand and contract.

EDIT: Hopefully it looks more like an expand/contract hierarchy now than strictly tabular data. I understand that it’s possible to do this with a table, it just seems less natural to present a hierarchy that way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:31:37+00:00Added an answer on June 14, 2026 at 1:31 pm

    With the following HTML you can do this (and, frankly, with other HTML you could do this…) but you should use tables.

    Anyway, that said, the HTML I’m working with:

    <ul>
        <li class="head">
            <span class="col1">Food</span>
            <span class="col2">Calories</span>
        </li>
        <li>Fruits
            <ul>
                <li>
                    <span class="col1">Apple</span>
                    <span class="col2">90</span>
                </li>
                <li>
                    <span class="col1">Grape</span>
                    <span class="col2">5</span>
                </li>
                <li>
                    <span class="col1">strawberry</span>
                    <span class="col2">16</span>
                </li>
            </ul></li>
        <li>Vegetable
            <ul>
                <li>
                    <span class="col1">Cucumber</span>
                    <span class="col2">12</span>
                </li>
                <li>
                    <span class="col1">Onion</span>
                    <span class="col2">29</span>
                </li>
            </ul></li>
    </ul>​
    

    And the CSS:

    li.head {
        font-weight: bold;
    }
    span.col1,
    span.col2 {
        display: inline-block;
        width: 48%;
    }
    
    ul > li > ul > li {
        padding-left: 10%;
        height: 0;
        line-height: 0;
        overflow: hidden;
        -webkit-transition: all 1s linear;
    }
    
    ul > li:hover > ul > li {
        height: 2em;
        line-height: 2em;
        -webkit-transition: all 1s linear;
    }
    
    ul > li > ul > li span:first-child::before {
        content: '-';
        width: 40%;
    }
    
    li li:nth-child(odd) span {
        background-color: #aaf;
    }
    ​
    

    JS Fiddle demo.

    To allow for keyboard navigation, and showing the nested foods/calorie values in response to tab-events, I’ve amended the HTML a little to wrap the fruits and vegetable text with an a element:

    <ul>
        <li class="head">
            <span class="col1">Food</span>
            <span class="col2">Calories</span>
        </li>
        <li><a href="#">Fruits</a>
            <ul>
                <li>
                    <span class="col1">Apple</span>
                    <span class="col2">90</span>
                </li>
                <li>
                    <span class="col1">Grape</span>
                    <span class="col2">5</span>
                </li>
                <li>
                    <span class="col1">strawberry</span>
                    <span class="col2">16</span>
                </li>
            </ul></li>
        <li><a href="#">Vegetable</a>
            <ul>
                <li>
                    <span class="col1">Cucumber</span>
                    <span class="col2">12</span>
                </li>
                <li>
                    <span class="col1">Onion</span>
                    <span class="col2">29</span>
                </li>
            </ul></li>
    </ul>​
    

    With the following CSS:

    li.head {
        font-weight: bold;
    }
    
    li a {
        color: inherit;
        text-decoration: none;
    }
    
    span.col1,
    span.col2 {
        display: inline-block;
        width: 48%;
    }
    
    ul > li > ul > li {
        padding-left: 10%;
        height: 0;
        line-height: 0;
        overflow: hidden;
        -moz-transition: all 1s linear;
        -ms-transition: all 1s linear;
        -o-transition: all 1s linear;
        -webkit-transition: all 1s linear;
        transition: all 1s linear;
    }
    
    ul > li a:focus + ul > li,
    ul > li:hover > ul > li {
        height: 2em;
        line-height: 2em;
        -moz-transition: all 1s linear;
        -ms-transition: all 1s linear;
        -o-transition: all 1s linear;
        -webkit-transition: all 1s linear;
        transition: all 1s linear;
    }
    
    ul > li > ul > li span:first-child::before {
        content: '-';
        width: 40%;
    }
    
    li li:nth-child(odd) span {
        background-color: #aaf;
    }
    ​
    

    JS Fiddle demo.

    Both of these, however, assume that you want to hide automatically, and show based on user-interaction. If that assumption’s incorrect then the transitions aren’t necessary.

    Incidentally, an accordion-like table solution:

    <table>
        <colgroup>
            <col class="foods" />
            <col class="calories" />
        </colgroup>
        <thead>
            <tr>
                <th>Food</th>
                <th>Calories</th>
            </tr>
        </thead>
        <tbody>
            <tr class="header">
                <td colspan="2">Fruits</td>
            </tr>
            <tr>
                <td>Apple</td>
                <td>90</td>
            </tr>
            <tr>
                <td>Grape</td>
                <td>5</td>
            </tr>
            <tr>
                <td>Strawberry</td>
                <td>16</td>
            </tr>
        </tbody>
        <tbody>
            <tr class="header">
                <td colspan="2">Vegetable</td>
            </tr>
            <tr>
                <td>Cucumber</td>
                <td>12</td>
            </tr>
            <tr>
                <td>Onion</td>
                <td>29</td>
            </tr>
        </tbody>
    </table>
    

    CSS:

    .foods,
    .calories {
        width: 8em;
    }
    
    tbody tr.header {
        height: 2em;
        line-height: 2em;
    }
    
    tbody tr,
    tbody tr td {
        max-height: 0;
        line-height: 0;
        overflow: hidden;
        -moz-transition: all 1s linear;
        -ms-transition: all 1s linear;
        -o-transition: all 1s linear;
        -webkit-transition: all 1s linear;
        transition: all 1s linear;
    }
    
    tbody tr td:first-child {
        padding-left: 2em;
    }
    
    tbody tr.header td {
        padding: 0;
    }
    
    tbody:hover tr {
        height: 2em;
        max-height: 2em;
        line-height: 2em;
        -moz-transition: all 1s linear;
        -ms-transition: all 1s linear;
        -o-transition: all 1s linear;
        -webkit-transition: all 1s linear;
        transition: all 1s linear;
    }
    ​
    

    JS Fiddle demo.

    This makes the same assumptions as previously, that you want to control the visibility by hovering over the header to show the hidden content.

    And, just for kicks, adding keyboard-navigation (with tab), using the tabindex attribute on the tr.header elements:

    <table>
        <colgroup>
            <col class="foods" />
            <col class="calories" />
        </colgroup>
        <thead>
            <tr>
                <th>Food</th>
                <th>Calories</th>
            </tr>
        </thead>
        <tbody>
            <tr class="header" tabindex="1">
                <td colspan="2">Fruits</td>
            </tr>
            <!-- unchanged from the previously-posted table mark-up -->
        </tbody>
        <tbody>
            <tr class="header" tabindex="2">
                <td colspan="2">Vegetable</td>
            </tr>
            <!-- unchanged from the previously-posted table mark-up -->
        </tbody>
    </table>
    

    ​And CSS:

    /* Other CSS remains the same */
    tr.header:focus ~ tr,
    tbody:hover tr {
        height: 2em;
        max-height: 2em;
        line-height: 2em;
        -moz-transition: all 1s linear;
        -ms-transition: all 1s linear;
        -o-transition: all 1s linear;
        -webkit-transition: all 1s linear;
        transition: all 1s linear;
    }
    
    tr.header:focus {
        outline: none;
        font-style: italic;
        background-color: #ffa;
    }
    ​
    

    JS Fiddle demo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to format a list into a string in this way: [1,2,3] =>
How do I convert an unordered list in this format <ul class=selectdropdown> <li><a href=one.html
I want to format any text that is placed into a NSTextField/UITextField to look
I want to format a string before assigning it to a label's text property
I want to format a datetime field but i can't figure out the syntactic.
I want to do something like this Can you transform unordered xml to match
I'm trying to create an unordered list of <a>text1 text2 text3</a> elements, with a
I want to format a number like this : 100000000 => 100 000 000
I want to format a number to have two digits. The problem is caused
I want to format srt subtitle text files to avoid wrapping problems on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.