How to make a list with each row divided into two columns, with the first column growing to the size of its contents?

前端 未结 1 1641
星月不相逢
星月不相逢 2021-01-22 01:54

Originally I needed this for a chat app. Now I need it for something else as well... I figure I\'d better ask.

Elaborating the chat app example: chat messages are

1条回答
  •  不思量自难忘°
    2021-01-22 02:39

    You are almost good with your grid solution. You can keep the li element by introducing display:contents (https://caniuse.com/#feat=css-display-contents) but you will still not be able to style the li since this one will no more generate a box content:

    ul {
      display: grid;
      grid-template-columns: repeat(2, auto);
    }
    li {
      display:contents;
    }
    
    .author {
      font-weight: bold;
      margin-right: 5px;
      text-align: right;
    }
    
    .author:after {
      content: ":";
    }
    
    span {
      margin-top: 10px;
    }
    • nickDon't you Remember, the Fifth of November, 'twas Gunpowder Treason Day, I let off my gun, and made'em all run. And Stole all their Bonfire away.
    • OtherNickMiss Susie had a steamboat, the steamboat had a bell. The steamboat went to Heaven, Miss Susie went to ...

    Another way is to consider display:table like below:

    ul {
      display: table;
    }
    
    li {
      display: table-row;
    }
    
    .author {
      font-weight: bold;
      padding-right: 5px;
      text-align: right;
    }
    
    .author:after {
      content: ":";
    }
    
    span {
      padding-top: 10px;
      display: table-cell;
    }
    • nickDon't you Remember, the Fifth of November, 'twas Gunpowder Treason Day, I let off my gun, and made'em all run. And Stole all their Bonfire away.
    • OtherNickMiss Susie had a steamboat, the steamboat had a bell. The steamboat went to Heaven, Miss Susie went to ...

    0 讨论(0)
提交回复
热议问题