3

I have a <ul>-list, where each list item contains multiple sections of text.

I want want each section of text to be as narrow as possible, but I still want all of them to be aligned vertically across the items. E.g.

<ul>
  <li> A. Name     | Some complex address        | 555-1234 </li>
  <li> Longer Name | Short Addr.                 | 555-1234 </li>
  <li> P. Diddy    | No home                     | 555-1234 </li>
</ul>

I hoped css grid would solve this issue for me, but I can't figure it out...

ul {
  list-style: none;
  display: grid;
  justify-content: stretch; 
}

li {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-column-gap: 10px;
}

p:nth-child(1) {
  background: #EEEEEE;
  border-right: 1px solid;
}

p:nth-child(2) {
   background: #CECECE;
   border-right: 1px solid;
}


p:nth-child(3) {
    background: #EEEEEE;
}
<ul>
  <li>
    <p>A. Name</p>
    <p>Some complex address</p>
    <p>555-1234</p>
  </li>
  <li>
    <p>Longer Name</p>
    <p>Short Addr.</p>
    <p>555-1234</p>
  </li>
  <li>
    <p>P. Diddy</p>
    <p>No home</p>
    <p>555-1234</p>
  </li>
</ul>

Is css grid the wrong tool for the job, or am I missing something here?

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Vegar
  • 12,828
  • 16
  • 85
  • 151
  • I know I'll get downvotes for this, but can fulfill your requirements.
    – ecg8 Apr 04 '18 at 22:18
  • yea, but `` comes with it's own baggage, though...
    – Vegar Apr 04 '18 at 22:30
  • You either need to flatten the HTML, so that all content items become siblings in the same container and can recognize the grid container lines (note that this practice is bad for HTML semantics)... – Michael Benjamin Apr 04 '18 at 22:59
  • 1
    ... or you can wait for browsers to implement Grid's [**subgrid feature**](https://stackoverflow.com/q/47929369/3597276), which allows descendants of a grid container, beyond the children, to recognize the top-level container lines. – Michael Benjamin Apr 04 '18 at 23:02
  • I tried subgrid multiple times before I spotted the 'this is not implemented'-note... :-( – Vegar Apr 05 '18 at 06:51
  • 1
    @Michael_B or wait for display: content, that probably will come before subgrid .... https://css-tricks.com/get-ready-for-display-contents/ – vals Apr 05 '18 at 08:01

2 Answers2

3

They need to belong to the same grid to have this result, so you may try something like this:

Yes there is no more list

.list {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-column-gap: 10px;
}

p:nth-child(3n + 1) {
  background: #EEEEEE;
  border-right: 1px solid;
}

p:nth-child(3n +2) {
  background: #CECECE;
  border-right: 1px solid;
}

p:nth-child(3n+3) {
  background: #EEEEEE;
}
<div class="list">
  <p>A. Name</p>
  <p>Some complex address</p>
  <p>555-1234</p>

  <p>Longer Name</p>
  <p>Short Addr.</p>
  <p>555-1234</p>

  <p>P. Diddy</p>
  <p>No home</p>
  <p>555-1234</p>
</div>

Or consider display:table and you can keep your list structure:

.list {
  display: table;
  margin:0;
  padding:0;
  border-spacing: 10px;
}
.list li {
  display:table-row;
}
p {
 display:table-cell;
}
p:nth-child(1) {
  background: #EEEEEE;
  border-right: 1px solid;
  white-space:nowrap;
}

p:nth-child(2) {
  background: #CECECE;
  border-right: 1px solid;
  width:100%;
}

p:nth-child(3) {
  background: #EEEEEE;
  white-space:nowrap;
}
<ul class="list">
<li>
  <p>A. Name</p>
  <p>Some complex address</p>
  <p>555-1234</p>
</li>
<li>
  <p>Longer Name</p>
  <p>Short Addr.</p>
  <p>555-1234</p>
</li>
<li>
  <p>P. Diddy</p>
  <p>No home</p>
  <p>555-1234</p>
</li>
</ul>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • maybe `display: table` is the right way. I want to keep the hierarchical structure. Typically, each list item would be a react component, and even though it's possible to have the component render as a set of top level `

    `-s instead of a `

  • ` containing childs, I'm not sure I want to go there...
  • – Vegar Apr 04 '18 at 22:33
  • @Vegar yes but not yet supported https://stackoverflow.com/questions/48613691/equal-height-children-of-grid-items/48613967#48613967 – Temani Afif Apr 04 '18 at 22:54
  • I agree absolutely on "it should be a table". Or at least, use display:table. (your second snippet). This should be the accepted answer. – vals Apr 06 '18 at 09:42
  • I agree that this is the best answer. I'm still a little worried of the limitations there, though. Things that requires 'hacks' where one item spans multiple table rows to get data spread vertically in normal tables will need the same 'hack' here, I would guess. My solution will be to use a grid on item level, and align items by having fixed size columns instead. That will work ok for my current case at least. – Vegar Apr 09 '18 at 10:08