如何为网格中的项目指定首选列

发布于 2025-02-10 18:41:00 字数 1265 浏览 1 评论 0原文

我想让我的blue项目更喜欢放在左侧,我所有的red> red项目更喜欢放置在右侧。所有项目都应从第一行开始,如果有更多类型,则应该能够使用另一列。

在@Temani AFIF的一些帮助下,我尝试了此操作,但由于它们没有包裹到另一列而没有起作用。

.grid {
  display: grid;
  grid-auto-flow: dense;
  width: 300px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-gap: .25rem;
}

.foo {
  background: blue;
  grid-column-start: 1;
  grid-column-end: 1;
}
.bar {
  background: red;
  grid-column-start: 2;
  grid-column-end: 2;
}

.item {
  padding: 0.5rem 1rem;
  color: white;
}
<div class="grid">
  <div class="item foo">Item 1</div>
  <div class="item foo">Item 3</div>
  <div class="item foo">Item 5</div>
  <div class="item foo">Item 7</div>
  <div class="item foo">Item 9</div>
  <div class="item bar">Item 2</div>
  <div class="item bar">Item 4</div>
  <div class="item bar">Item 6</div>
</div>

我想要的就是这样:

CSS是否可以?

I want to have my blue items prefer to be placed on the left side and all my red items prefer to be placed the right side. All items should start from the first row and if there are more one type, they should be able to use the other column.

With some help from @Temani Afif, I tried this, but it doesn't work as they are not wrapping into the other column.

.grid {
  display: grid;
  grid-auto-flow: dense;
  width: 300px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-gap: .25rem;
}

.foo {
  background: blue;
  grid-column-start: 1;
  grid-column-end: 1;
}
.bar {
  background: red;
  grid-column-start: 2;
  grid-column-end: 2;
}

.item {
  padding: 0.5rem 1rem;
  color: white;
}
<div class="grid">
  <div class="item foo">Item 1</div>
  <div class="item foo">Item 3</div>
  <div class="item foo">Item 5</div>
  <div class="item foo">Item 7</div>
  <div class="item foo">Item 9</div>
  <div class="item bar">Item 2</div>
  <div class="item bar">Item 4</div>
  <div class="item bar">Item 6</div>
</div>

What I want is something like this:

enter image description here

Here the red items start from the first row like the blue items and the blue items can wrap into the second column if there is an uneven amount of items.

Is this possible with css?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事未完 2025-02-17 18:41:00

仅指定红色的位置,并使用网格 - 自动流量:密度。那么诀窍是考虑order

.grid {
  display: grid;
  width: 300px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-auto-flow: dense;
  grid-gap: .25rem;
}

.foo {
  background: blue;
}

.bar {
  background: red;
  grid-column-start: 2;
  order: -1;
}

.item {
  padding: 0.5rem 1rem;
  color: white;
}
<div class="grid">
  <div class="item foo">Item 1</div>
  <div class="item foo">Item 3</div>
  <div class="item foo">Item 5</div>
  <div class="item foo">Item 7</div>
  <div class="item foo">Item 9</div>
  <div class="item bar">Item 2</div>
  <div class="item bar">Item 4</div>
  <div class="item bar">Item 6</div>
  <div class="item foo">Item 10</div>
  <div class="item foo">Item 11</div>
</div>

Specify the position of the red only and use grid-auto-flow: dense. Then the trick is to consider order as well

.grid {
  display: grid;
  width: 300px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-auto-flow: dense;
  grid-gap: .25rem;
}

.foo {
  background: blue;
}

.bar {
  background: red;
  grid-column-start: 2;
  order: -1;
}

.item {
  padding: 0.5rem 1rem;
  color: white;
}
<div class="grid">
  <div class="item foo">Item 1</div>
  <div class="item foo">Item 3</div>
  <div class="item foo">Item 5</div>
  <div class="item foo">Item 7</div>
  <div class="item foo">Item 9</div>
  <div class="item bar">Item 2</div>
  <div class="item bar">Item 4</div>
  <div class="item bar">Item 6</div>
  <div class="item foo">Item 10</div>
  <div class="item foo">Item 11</div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文