【问题标题】:Scroll doesn't work when grid item aligned to the end / bottom当网格项目对齐到末尾/底部时,滚动不起作用
【发布时间】:2018-04-01 13:00:21
【问题描述】:

我想使用CSS grid 创建聊天,但遇到了无法组合align-self: end;overflow-y: auto; 的问题。

ul 包含所有消息,应该与底部对齐,这样即使只有一条消息,它也会出现在底部。

聊天的页眉和页脚是固定的,您只能滚动浏览消息(如 Whats App 和类似应用程序)。

我用一些消息创建了this pen。消息通过标题,它永远不允许我滚动。

html,
body {
  margin: 0;
  height: 100%;
}

#chat {
  display: grid;
  height: inherit;
  grid-template-rows: 50px auto 50px;
}

header,
footer {
  background: #4081ff;
}

ul {
  overflow-y: auto;
  align-self: end;
}
<section id="chat">
  <header>header</header>
  <ul>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>Message 1</li>
    <li>last message</li>
  </ul>
  <footer>footer</footer>
</section>

【问题讨论】:

    标签: css overflow css-grid


    【解决方案1】:

    这似乎是一个错误。

    分析

    您正在使用align-self: end 将中间网格项 (ul) 固定到行的底部。无法呈现垂直滚动条。

    我尝试了各种替代方案:

    1. 我使用flex-direction: columnjustify-content: flex-endul 网格项设为弹性容器。

    2. 我使用flex-direction: columnul 网格项设为弹性容器,并将margin-top: auto 应用于第一个li 弹性项。

    3. 我尝试过切换

      grid-template-rows: 50px auto 50px
      

      grid-template-rows: 50px 1fr 50px
      

    没有任何效果。在所有情况下,浏览器都无法生成垂直滚动条。

    但是,在所有情况下,当没有“结束”对齐(换句话说,网格项与默认的“开始”位置对齐)时,垂直滚动可以正常工作。

    由于滚动条在“开始”中有效,但在“结束”中失败,我认为将其称为错误是公平的。

    flexbox 中也有同样的问题:Can't get vertical scrollbar with justify-content: flex-end


    解决方案 #1:仅限 Chrome

    如果您使用align-content: endul 网格项设为(嵌套)网格容器,这似乎在Chrome 中有效,但在Firefox 和Edge 中无效。我没有在 Safari、IE11 或其他浏览器中测试。

    #chat {
      display: grid;
      height: 100vh;
      grid-template-rows: 50px 1fr 50px;
    }
    
    ul {
      overflow-y: auto;
      display: grid;
      align-content: end;
    }
    
    header,
    footer {
      background: #4081ff;
    }
    
    body {
      margin: 0;
    }
    <section id="chat">
      <header>header</header>
      <ul>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>last message</li>
      </ul>
      <footer>footer</footer>
    </section>

    revised codepen


    解决方案 #2:所有浏览器

    此解决方案利用了垂直滚动对start 位置中的项目正常工作这一事实。您可以插入另一个li,或者更好的是,一个伪元素(因为它们在应用于网格容器时被视为网格项)。此li 将为空并设置为占用所有可用空间,这会将实际项目固定在底部。

    #chat {
      display: grid;
      height: 100vh;
      grid-template-rows: 50px auto 50px;
    }
    
    ul {
      overflow-y: auto;
      display: grid;
      grid-template-rows: 1fr; /* applies to the first row, the only explicit row;
                                  all subsequent rows, which are implicit rows, will
                                  take `grid-auto-rows: auto` by default */
    }
    
    ul::before {
      content: "";
      border: 1px dashed red; /* demo only */
    }
    
    header,
    footer {
      background: #4081ff;
    }
    
    body {
      margin: 0;
    }
    <section id="chat">
      <header>header</header>
      <ul>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>Message 1</li>
        <li>last message</li>
      </ul>
      <footer>footer</footer>
    </section>

    revised codepen

    【讨论】:

      【解决方案2】:

      我的滚动条解决方案。我添加了额外的 div。

      https://codepen.io/anon/pen/zWaRJY

      <section id="chat">
        <header>header</header>
        <div id="content">
        <ul>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>Message 1</li>
          <li>last message</li>
        </ul>
        </div>
        <footer>footer</footer>
      </section>
      
      html, body {
        margin: 0;
        height: 100%;
      }
      
      #chat {
        display: grid;
        height: inherit;
        grid-template-rows: 50px auto 50px;
      }
      
      header, footer {
        background: #4081ff;
      }
      
      #content {
        overflow-y: auto;
      }
      
      ul {
        display: flex;
        flex-direction: column;
        justify-content: end;
        min-height: 100%;
        margin: 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-03
        • 1970-01-01
        • 2014-10-12
        • 2021-02-23
        • 2013-08-15
        • 2017-11-13
        • 2023-02-01
        • 2021-06-23
        相关资源
        最近更新 更多