Home > Software engineering >  CSS Nested Lists as tables?
CSS Nested Lists as tables?

Time:11-19

I have the following list structure:

<ul class="clientlist">
    <li class="clientname">
        <span class="id">IDxxx</span><span class="name">Hugo</span><span class='clientcount'>15</span>
        <ul class="fbllist">
            <li class="fbladdress">
                <span class="address">[email protected]</span><span class="fblcount">15</span>
                <ul class="iplist">
                    <li class="ipstatus">
                        <span class="statustext">active</span><span class="statuscount">15</span>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

which is nicely displayed, thanks to CSS like:

Of course there are several Elements like this, so a list may look like this:

Now I'm trying to find a way to format this more "table like" and tried this css

ul {
  display: table;
}

li {
  display: table-row;
}

li span {
  display: table-cell;
}

span.ipcount,
span.clientcount,
span.statuscount {
  text-align: right;
}
<ul class="clientlist">
  <li class="clientname">
    <span class="id">IDxxx</span><span class="name">Hugo</span><span class='clientcount'>15</span>
    <ul class="fbllist">
      <li class="fbladdress">
        <span class="address">[email protected]</span><span class="fblcount">15</span>
        <ul class="iplist">
          <li class="ipstatus">
            <span class="statustext">active</span><span class="statuscount">15</span>
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Unfortunately, due to the fact that the uls are nested within lis, the result is not quite what I hoped for. All the "sub-tables" (the nested uls) are shown at the very right end of each "row" (li).

Is there any way I can change it so that each "sub-table" is displayed like being in a new row without changing the structure?

Update: This is what it looks like when I add borders to the tables (black) and cells (green) enter image description here

I hoped for something like this: enter image description here

CodePudding user response:

You can make a mix of table/grid and contents display to achieve something mostly alike your goal:

example

.clientlist,
li,
span {
  box-shadow: 0 0 0 1px;/* or border */
}

.clientlist {
  display: table;
}

.clientname {
  display: grid;
  grid-template-columns: 1fr auto auto;
}

.ipstatus {
  display: contents;
}

.fbladdress,
.iplist {
  display: grid;
  grid-template-columns: 1fr auto
}
<ul class="clientlist">
  <li class="clientname">
    <span class="id">IDxxx1</span><span class="name">Hugo</span><span class='clientcount'>15</span>
    <ul class="fbllist">
      <li class="fbladdress">
        <span class="address">[email protected]</span><span class="fblcount">15</span>
        <ul class="iplist">
          <li class="ipstatus">
            <span class="statustext">active</span><span class="statuscount">15</span>
          </li>
        </ul>
      </li>
    </ul>
  </li>
  <li class="clientname">
    <span class="id">IDxxx2</span><span class="name">Hugo2</span><span class='clientcount'>15</span>
    <ul class="fbllist">
      <li class="fbladdress">
        <span class="address">[email protected]</span><span class="fblcount">14</span>
        <ul class="iplist">
          <li class="ipstatus">
            <span class="statustext">active</span><span class="statuscount">13</span>
          </li>
          <li class="ipstatus">
            <span class="statustext">monitored</span><span class="statuscount">1</span>
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related