我正在尝试学习flexbox,但是我正在努力理解该概念。我想创建一个电子邮件列表,其中每个元素都是一个flexbox容器(请参阅下面的内容)。

html - Flexbox列表,其中的剩余宽度,省略号和新行中的一些内容-LMLPHP

规则:

  • 标签是可选的,但如果显示,它将直接显示在名称旁边。
  • 日期总是存在,宽度会变化
  • 该人的姓名填补了剩余的空白。如果变大,它将被截断。
  • 名称和标签之间的间隔始终为5px
  • 邮件主题显示在新行上,并且也被截断了

  • 到目前为止,我的尝试:https://jsbin.com/sepobipiwa/edit?html,css,output

    .container {
      width: 350px;
      background-color: #FFF;
    }
    html {
      font-family: 'Nunito Sans', sans-serif;
    }
    ul {
      list-style-type: none;
      padding: 0;
      margin: 0;
    }
    li {
      display: flex;
      flex-direction: row;
      align-items: baseline;
      padding: 20px;
      border-bottom: 1px solid #ccc;
    }
    .name {
      font-size: 16px;
      font-weight: bold;
      text-overflow: ellipsis;
    }
    .tag {
      margin-left: 5px;
      font-size: 10px;
      font-weight: bold;
      background-color: #EFEFEF;
      padding: 3px 5px;
      border-radius: 3px;
      text-transform: uppercase;
    }
    .subject {
      font-size: 15px;
      font-weight: 300;
      color: #888;
      text-overflow: ellipsis;
    }
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <link href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,600,700,800" rel="stylesheet">
      <title>JS Bin</title>
    </head>
    
    <body>
      <div class="container">
        <ul>
          <li>
            <div class="name">John Doe</div>
            <div class="date">14:50</div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
          <li>
            <div class="name">Marrie Antoinette</div>
            <div class="tag">Concept</div>
            <div class="date">16:01</div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
          <li>
            <div class="name">Someone with a long name</div>
            <div class="tag">tag</div>
            <div class="date">18:50</div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
          <li>
            <div class="name">Someone with a long name</div>
            <div class="tag">concept</div>
            <div class="date">yesterday</div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
        </ul>
      </div>
    </body>
    
    </html>

    最佳答案

    因此,我对您的代码执行了此操作:

  • 通过将flexbox添加到flex-wrap: wrap来使您的li包装
  • 告诉subject采取行中的全部可用宽度,从而迫使flex-basis: 100%进入下一行,并为其指定margin-left: auto
  • 添加date以将name放置在最右端。
  • 要完成它,您可以将以下样式添加到subjectmax-width: 50%中,以使省略号醒来:
      white-space: nowrap;
      overflow: hidden;
    
  • 还向name中添加了name以调整第一行。

  • 请参见下面的演示:

    .container {
      width: 350px;
      background-color: #FFF;
    }
    html {
      font-family: 'Nunito Sans', sans-serif;
    }
    ul {
      list-style-type: none;
      padding: 0;
      margin: 0;
    }
    li {
      display: flex;
      flex-wrap: wrap;
      flex-direction: row;
      align-items: baseline;
      padding: 20px;
      border-bottom: 1px solid #ccc;
    }
    .name {
      font-size: 16px;
      font-weight: bold;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      max-width: 50%;
    }
    .tag {
      margin-left: 5px;
      font-size: 10px;
      font-weight: bold;
      background-color: #EFEFEF;
      padding: 3px 5px;
      border-radius: 3px;
      text-transform: uppercase;
    }
    .subject {
      font-size: 15px;
      font-weight: 300;
      color: #888;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      flex-basis: 100%;
    }
    .date {
      margin-left: auto;
    }
    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <link href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,600,700,800" rel="stylesheet">
      <title>JS Bin</title>
    </head>
    
    <body>
      <div class="container">
        <ul>
          <li>
            <div class="name">John Doe</div>
            <div class="date">14:50</div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
          <li>
            <div class="name">Marrie Antoinette</div>
            <div class="tag">Concept</div>
            <div class="date">16:01</div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
          <li>
            <div class="name">Someone with a long name</div>
            <div class="tag">tag</div>
            <div class="date">18:50</div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
          <li>
            <div class="name">Someone with a long name</div>
            <div class="tag">concept</div>
            <div class="date">yesterday</div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
        </ul>
      </div>
    </body>
    
    </html>


    建议:

    您应该编辑标记,并为包含tagdatemax-width: 50%的第一行创建一个包装,这是一种更好的设计(您可以删除上面使用的繁琐的ojit_code)-请参见下面的演示:

    .container {
      width: 350px;
      background-color: #FFF;
    }
    html {
      font-family: 'Nunito Sans', sans-serif;
    }
    ul {
      list-style-type: none;
      padding: 0;
      margin: 0;
    }
    li {
      display: flex;
      flex-wrap: wrap;
      flex-direction: row;
      align-items: baseline;
      padding: 20px;
      border-bottom: 1px solid #ccc;
    }
    .name {
      font-size: 16px;
      font-weight: bold;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
    .tag {
      margin-left: 5px;
      font-size: 10px;
      font-weight: bold;
      background-color: #EFEFEF;
      padding: 3px 5px;
      border-radius: 3px;
      text-transform: uppercase;
    }
    .subject {
      font-size: 15px;
      font-weight: 300;
      color: #888;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      flex-basis: 100%;
    }
    .date {
      margin-left: auto;
    }
    .header {
      display: flex;
      width: 100%;
    }
    <!DOCTYPE html>
    <html>
    
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <link href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,600,700,800" rel="stylesheet">
      <title>JS Bin</title>
    </head>
    
    <body>
      <div class="container">
        <ul>
          <li>
            <div class="header">
              <div class="name">John Doe</div>
              <div class="date">14:50</div>
            </div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
          <li>
            <div class="header">
              <div class="name">Marrie Antoinette</div>
              <div class="tag">Concept</div>
              <div class="date">16:01</div>
            </div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
          <li>
            <div class="header">
              <div class="name">Someone with a long name</div>
              <div class="tag">tag</div>
              <div class="date">18:50</div>
            </div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
          <li>
            <div class="header">
              <div class="name">Someone with a long name</div>
              <div class="tag">concept</div>
              <div class="date">yesterday</div>
            </div>
            <div class="subject">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
          </li>
        </ul>
      </div>
    </body>
    
    </html>

    关于html - Flexbox列表,其中的剩余宽度,省略号和新行中的一些内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41491277/

    10-12 12:32
    查看更多