【问题标题】:how to wrap css grid columns to next row when there is no space没有空间时如何将css网格列包装到下一行
【发布时间】:2021-11-04 15:23:33
【问题描述】:

我目前遇到了 CSS 网格的问题。我在 Stackoverflow 或 Google 上找了几个小时,但找不到相关且正确的解决方案。

我的目标是实现这种行为:

一开始是一行中的所有元素,但是当我开始缩小屏幕(响应性)时,我想让不适合换行的元素换行。 (基本上是最后一个元素,在这种情况下是链接)。我想要实现的这种行为就像display flex的属性flex-wrap:wrap一样。

在我一直在搜索的所有文章中,它们都使用固定宽度的列,但我的目标也是让它像使用 display:flex 时一样“自动”。 (我真的不知道文本会有多长时间。文本将通过props发送到该组件)

总结一下:

我的目标是让display:grid 自动检测它必须创建多少列(那里有多少个子项)并在没有空间时自动将元素换行到新行。

这可能吗?

.notification{
  background:lightgreen;
  padding:1rem;
  display:inline-block;
}

.notification-body{
  display: inline-grid;
  grid-auto-flow: column;
  grid-column-gap: 1rem;
  align-items: center;
}

.notification-content-wrapper{
  display: inline-grid;
  grid-auto-flow: column;
  /*this is supposed to work according to other articles..*/
  grid-template-columns: repeat(auto-fit, minmax(max-content, 0px));
  grid-column-gap: 1rem;
  align-items: center;
}
<div class="notification">
  <div class="notification-body">
    <p>there will<br>be always icon</p>
    
    <div class="notification-content-wrapper">
      <p>Notification message</p>
      <a href="#">notification link</a>
    </div>
    
    <p>there will be<br>always icon too</p>
  </div>
</div>

【问题讨论】:

    标签: html css css-grid


    【解决方案1】:

    正如 Ricky 在此 (CSS grid wrapping) 所述:

    repeat( [ auto-fill | auto-fit ] , [ <line-names>? <fixed-size> ]+ <line-names>? )
    

    查看他们的答案以获得更详细的解释。

    【讨论】:

    • 我看过那个帖子,但我很不明白。 Ricky 也在使用固定宽度,我根本无法
    • 请提供一个工作示例,以便其他人可以解决问题并提出解决方案
    • 也许您正在尝试制作砖石网格? Mdn Masonry Grid我不知道为什么你不会有一些最大长度来帮助列换行
    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 2013-09-24
    • 2012-06-30
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    相关资源
    最近更新 更多