【问题标题】:CSS Grid populate items from the endCSS Grid 从最后填充项目
【发布时间】:2021-03-02 02:08:39
【问题描述】:

我有以下用于显示过滤器选项的网格:

+------------------------------------------------------------------------------------------+
|                                                                                          |
| +-----------+  +-----------+  +-----------+  +-----------+  +-----------+  +-----------+ |
| |     1     |  |     2     |  |     3     |  |     4     |  |     5     |  |     6     | |
| +-----------+  +-----------+  +-----------+  +-----------+  +-----------+  +-----------+ |
|                                                                                          |
+------------------------------------------------------------------------------------------+

这是相关的 CSS:

  min-height: 0; /* NEW */
  min-width: 0; /* NEW; needed for Firefox */
  display: grid;
  grid-gap: 1rem;
  grid-template-columns: repeat(6, 1fr);

  @media (max-width: 991.98px) {
    grid-template-columns: repeat(3, 1fr);
  }

  @media (max-width: 575.98px) {
    width: 100%;
    grid-gap: 0;
  }

现在我有一个新要求,要求我隐藏任何会导致空列表的过滤器选项。假设只显示项目123(尽管可以是任何数量的六个),那么目前我有:

+------------------------------------------------------------------------------------------+
|                                                                                          |
| +-----------+  +-----------+  +-----------+                                              |
| |     1     |  |     2     |  |     3     |                                              |
| +-----------+  +-----------+  +-----------+                                              |
|                                                                                          |
+------------------------------------------------------------------------------------------+

我需要的是这样显示:

+------------------------------------------------------------------------------------------+
|                                                                                          |
|                                              +-----------+  +-----------+  +-----------+ |
|                                              |     1     |  |     2     |  |     3     | |
|                                              +-----------+  +-----------+  +-----------+ |
|                                                                                          |
+------------------------------------------------------------------------------------------+

我可以使用 CSS 网格达到这个结果还是应该使用其他东西?

【问题讨论】:

  • 你试过align-items: end;这个容器吗?
  • 是的,那是我的第一次尝试。不幸的是,它不起作用。
  • 但是你使用的是没有类的属性吗?
  • 很确定你不能用 CSS 来管理它。它将在网格中放置不可见的项目,因此 JS 是您更好的选择。
  • @HenriqueBarcelos 的“渲染”是指“添加到 HTML”吗?因为 CSS 控制着网站的“渲染”。

标签: css css-grid


【解决方案1】:

由于它只有 6 列(或 3 列),您可以使用 nth-last-child 明确放置元素:

.box {
  min-height: 0;
  min-width: 0;
  display: grid;
  grid-gap: 1rem;
  grid-template-columns: repeat(6, 1fr);
  border:1px solid #000;
  margin:10px;
}
.box > * {
   font-size:20px;
   background:red;
   color:#fff;
}
@media (max-width: 991.98px) {
  .box {
    grid-template-columns: repeat(3, 1fr);
  }
}

.box > *:nth-last-child(1) { grid-column:-2}
.box > *:nth-last-child(2) { grid-column:-3}
.box > *:nth-last-child(3) { grid-column:-4}
.box > *:nth-last-child(4) { grid-column:-5}
.box > *:nth-last-child(5) { grid-column:-6}

@media (max-width: 991.98px) {
  .box > *:nth-last-child(4) { grid-column:-2}
  .box > *:nth-last-child(5) { grid-column:-3}
}
<div class="box">
  <div>1</div>
</div>

<div class="box">
  <div>1</div>
  <div>2</div>
</div>

<div class="box">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

<div class="box">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
</div>

<div class="box">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

<div class="box">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>

或者像下面这样:

.box {
  min-height: 0;
  min-width: 0;
  display: grid;
  grid-gap: 1rem;
  grid-template-columns: repeat(6, 1fr);
  border:1px solid #000;
  margin:10px;
}
.box > * {
   font-size:20px;
   background:red;
   color:#fff;
}
@media (max-width: 991.98px) {
  .box {
    grid-template-columns: repeat(3, 1fr);
  }
}

.box > *:nth-last-child(1) { grid-column:-2}
.box > *:nth-last-child(2) { grid-column:-3}
.box > *:nth-last-child(3) { grid-column:-4}
.box > *:nth-last-child(4) { grid-column:-5}
.box > *:nth-last-child(5) { grid-column:-6}

@media (max-width: 991.98px) {
  .box > *:nth-last-child(1):nth-child(1),
  .box > *:nth-last-child(1):nth-child(2) { grid-column:-2}
  .box > *:nth-last-child(2):nth-child(1) { grid-column:-3}
  
  .box > *:nth-last-child(1) { grid-column:auto}
  .box > *:nth-last-child(2) { grid-column:auto}
  .box > *:nth-last-child(3) { grid-column:auto}
  .box > *:nth-last-child(4) { grid-column:auto}
  .box > *:nth-last-child(5) { grid-column:auto}
}
<div class="box">
  <div>1</div>
</div>

<div class="box">
  <div>1</div>
  <div>2</div>
</div>

<div class="box">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

<div class="box">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
</div>

<div class="box">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

<div class="box">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 2021-06-08
    • 1970-01-01
    相关资源
    最近更新 更多