【问题标题】:Flex item is not shrinking smaller than its content [duplicate]Flex项目没有缩小到小于其内容[重复]
【发布时间】:2018-05-07 11:58:07
【问题描述】:

我有一个要向用户显示的元素列表以及一个图标和两个按钮。到目前为止一切顺利,但我希望此列表可以扩展到移动设备并在必要时缩小。

当列表中的文本太长时,它会阻止页面缩小并强制显示水平滚动条。我想要实现的是,长描述文本被缩小,最后显示 3 个点(省略号)。

容器元素显示为flex,文本容器有flex-shrink 1,但还是拒绝收缩溢出。

谁能指导我在这里做错了什么?为什么.mdc-list-item 拒绝收缩?有没有办法在必要时仅使用 CSS 强制它缩小?

.mdc-list-item {
  flex-shrink: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mdc-list {
  display: flex;
  flex-direction: column;
}
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.js"></script>
<link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.css" rel="stylesheet" />
<div style="width: 100%; max-width: 800px; margin: 0 auto; display: flex;">
  <ul class="mdc-list mdc-list--two-line mdc-elevation--z1" style="flex: 1;">
    <li class="mdc-list-item" title="Test Item 1 Description" channel-id="1">
      <img class="mdc-list-item__start-detail grey-bg" style="width: 40px; height: 40px;" src="https://material-components-web.appspot.com/images/animal3.svg" alt="Brown Bear">

      <span class="mdc-list-item__text">
        Test Item 1
        <span class="mdc-list-item__text__secondary">Test Item 1 Description</span>
      </span>

      <div class="mdc-list-item__end-detail">
        <i class="mdc-icon-toggle material-icons color-primary-text-inv toggle-notifications-email" style="margin-top: -12px;" role="button">
          X
        </i>
      </div>
      <div class="mdc-list-item__end-detail" style="margin-left: 64px;">
        <i class="mdc-icon-toggle material-icons color-primary-text-inv toggle-notifications-notification" style="margin-top: -12px; margin-left: -24px;" role="button">
          Y
        </i>
      </div>
    </li>
    <li role="separator" class="mdc-list-divider"></li>
    <li class="mdc-list-item" title="Here you can read the long description of Item 2 which refuses to shrink" channel-id="2">
      <img class="mdc-list-item__start-detail grey-bg" style="width: 40px; height: 40px;" src="https://material-components-web.appspot.com/images/animal3.svg" alt="Brown Bear">

      <span class="mdc-list-item__text">
        Test Item 2
        <span class="mdc-list-item__text__secondary">Here you can read the long description of Item 2 which refuses to shrink</span>
      </span>

      <div class="mdc-list-item__end-detail">
        <i class="mdc-icon-toggle material-icons color-primary-text-inv toggle-notifications-email" style="margin-top: -12px;" role="button">
          X
        </i>
      </div>
      <div class="mdc-list-item__end-detail" style="margin-left: 64px;">
        <i class="mdc-icon-toggle material-icons color-primary-text-inv toggle-notifications-notification" style="margin-top: -12px; margin-left: -24px;" role="button">
          Y
        </i>
      </div>
    </li>
  </ul>
</div>

【问题讨论】:

    标签: html css flexbox ellipsis


    【解决方案1】:

    有时您需要查看所有 flex 项(上下HTML 结构),并检查它们是否需要overflow / min-width 覆盖。

    在这种情况下,更高级别的弹性项目仍默认为min-width: auto,从而阻止了大小的减小。

    .mdc-list-item {
      flex-shrink: 1;
      min-width: 0;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .mdc-list {
      display: flex;
      flex-direction: column;
    }
    
    /* RULES ADDED */
    .mdc-list {
       min-width: 0;
    }
    .mdc-list-item__text {
      overflow: hidden;
    }
    .mdc-list-item__text__secondary {
      text-overflow: ellipsis;
      overflow: hidden;
    }
    <script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.js"></script>
    <link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.css" rel="stylesheet" />
    <div style="width: 100%; max-width: 800px; margin: 0 auto; display: flex;">
      <ul class="mdc-list mdc-list--two-line mdc-elevation--z1" style="flex: 1;">
        <li class="mdc-list-item" title="Test Item 1 Description" channel-id="1">
          <img class="mdc-list-item__start-detail grey-bg" style="width: 40px; height: 40px;" src="https://material-components-web.appspot.com/images/animal3.svg" alt="Brown Bear">
    
          <span class="mdc-list-item__text">
            Test Item 1
            <span class="mdc-list-item__text__secondary">Test Item 1 Description</span>
          </span>
    
          <div class="mdc-list-item__end-detail">
            <i class="mdc-icon-toggle material-icons color-primary-text-inv toggle-notifications-email" style="margin-top: -12px;" role="button">
              X
            </i>
          </div>
          <div class="mdc-list-item__end-detail" style="margin-left: 64px;">
            <i class="mdc-icon-toggle material-icons color-primary-text-inv toggle-notifications-notification" style="margin-top: -12px; margin-left: -24px;" role="button">
              Y
            </i>
          </div>
        </li>
        <li role="separator" class="mdc-list-divider"></li>
        <li class="mdc-list-item" title="Here you can read the long description of Item 2 which refuses to shrink" channel-id="2">
          <img class="mdc-list-item__start-detail grey-bg" style="width: 40px; height: 40px;" src="https://material-components-web.appspot.com/images/animal3.svg" alt="Brown Bear">
    
          <span class="mdc-list-item__text">
            Test Item 2
            <span class="mdc-list-item__text__secondary">Here you can read the long description of Item 2 which refuses to shrink</span>
          </span>
    
          <div class="mdc-list-item__end-detail">
            <i class="mdc-icon-toggle material-icons color-primary-text-inv toggle-notifications-email" style="margin-top: -12px;" role="button">
              X
            </i>
          </div>
          <div class="mdc-list-item__end-detail" style="margin-left: 64px;">
            <i class="mdc-icon-toggle material-icons color-primary-text-inv toggle-notifications-notification" style="margin-top: -12px; margin-left: -24px;" role="button">
              Y
            </i>
          </div>
        </li>
      </ul>
    </div>

    【讨论】:

    • 我真的很感谢斜体 all。它让我检查每一个并找到它。
    • 如果我们不希望溢出但文本断字,解决方案是什么。
    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 2016-11-17
    • 1970-01-01
    • 2017-12-16
    • 2020-03-22
    • 1970-01-01
    • 2020-04-07
    • 2021-03-10
    相关资源
    最近更新 更多