Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Horizontal UL where LI contains a TABLE. Possible?

I am trying to fix a horrid nested table layout for a site. The page will have a variable number of elements that leverage Google charts. Instead of complex spaghetti code that tries to lay things out inside of table cells I want to use a horizontal UL so the content blocks will lay out cleanly regardless of the charts involved. The problem I am having is the Google charts components leverage tables. When a table element exists anywhere inside a LI the LI gets moved to the next line (assuming because table elements by default have a newline before and after).

I have tried the various display modes for the table with no luck. Is this a lost cause?

Example HTML code to illustrate the issue:

<html>
<body>
<style type='text/css'>
 #navlist li{
    display:inline;
    list-style-type:none;

    }
</style>
    <ul id='navlist'>
        <li>TEST</li>
        <li>TEST2</li>
        <li>
            <table style='border:1px solid black'><tr><td>TEST</td></tr></table>
        </li>
        <li>TEST3</li>
        <li>
            <table style='border:1px solid blue'><tr><td>TEST</td></tr></table>
        </li>
        <li>
            <table style='border:1px solid green'><tr><td>TEST</td></tr></table>
        </li>
    </ul>
</body>
</html>
like image 612
Tim Reynolds Avatar asked Sep 15 '10 14:09

Tim Reynolds


People also ask

How do you display ul li horizontally?

If you want to make this navigational unordered list horizontal, you have basically two options: Make the list items inline instead of the default block. .li { display: inline; } This will not force breaks after the list items and they will line up horizontally as far as they are able. Float the list items.

Can we use UL in table?

Yes it's because tables are by default block elements (well, actually display:table but it acts in a similar manner).

How do you use UL in a table?

The <ul> tag defines an unordered (bulleted) list. Use the <ul> tag together with the <li> tag to create unordered lists. Tip: Use CSS to style lists. Tip: For ordered lists, use the <ol> tag.

Can I put UL inside Li?

You must wrap every inner UL s with an LI , i.e. Show activity on this post. The children (direct descendants) of a ul element must all be li elements. This is a purely syntactic requirement.


1 Answers

Set display: inline-block; on your LI elements; that should do it nicely. It doesn't really work in Firefox 2, but nobody uses Firefox 2 anymore. You'll need to specify a doctype to get it to work in IE.

<!DOCTYPE html>
<html>
  <head>
    <style type='text/css'>
      #navlist li {
        display: inline-block;
        zoom: 1;
        *display: inline;
        list-style-type: none;
        vertical-align: middle;
      }
    </style>
  </head>
  <body>
    <ul id='navlist'>
      <li>TEST</li>
      <li>TEST2</li>
      <li>
        <table style='border:1px solid black'><tr><td>TEST</td></tr></table>
      </li>
      <li>TEST3</li>
      <li>
        <table style='border:1px solid blue'><tr><td>TEST</td></tr></table>
      </li>
      <li>
        <table style='border:1px solid green'><tr><td>TEST</td></tr></table>
      </li>
    </ul>
  </body>
</html>
like image 183
Chris Heald Avatar answered Sep 29 '22 03:09

Chris Heald