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.
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:
And the CSS:
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
fruitsandvegetabletext with anaelement:With the following CSS:
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
tablesolution:CSS:
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
tabindexattribute on thetr.headerelements:And CSS:
JS Fiddle demo.