如何在CSS网格中创建重复的模式

huangapple go评论62阅读模式
英文:

How can I make a repeated pattern in a css grid

问题

我试图制作一个如下图案的 CSS 网格:

  • 每行显示 4 个元素,然后显示 2 个元素,然后显示 12 个元素(作为每行的 4 个元素),最后显示 2 个元素(每行 2 个元素),然后重复整个模式。

这是第一个迭代的图片:
如何在CSS网格中创建重复的模式

我尝试使用 CSS 网格解决方案,但遇到了一点困难。
FiddleJS: https://jsfiddle.net/g15nyto6/42/

.parent {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-column-gap: 5px;
  grid-row-gap: 5px;
  grid-auto-flow: dense;
}

.child {
  background: tomato;
  width: 100%;
  height: 100%;
}

.child:nth-child(-n + 2) {
  background: cadetblue;
  grid-column: span 2;
  grid-row: span 2;
}

.child:nth-child(5n) {
  background: pink;
  grid-column: span 1;
  grid-row: span 2;
}
<div class="parent">
  <div class="child">child-1</div>
  <div class="child">child-2</div>
  <!-- 其他子元素... -->
</div>

任何帮助将不胜感激。

英文:

I'm trying to make a css grid of the following pattern

  • display 4 elements in a row, then display 2 in row, then 12 (as 4 in row), and last 2 (in a row), then repeat the whole pattern again.
    This is a picture of the first iteration:
    如何在CSS网格中创建重复的模式

I'm trying the solution with css grid, was bit stuck
FiddleJS: https://jsfiddle.net/g15nyto6/42/

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-css -->

.parent {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  /* grid-auto-rows: 50px; */
  grid-column-gap: 5px;
  grid-row-gap: 5px;
  grid-auto-flow: dense;
}

.child {
  background: tomato;
  width: 100%;
  height: 100%;
}

.child:nth-child(-n + 2) {
  background: cadetblue;
  grid-column: span 2;
  grid-row: span 2;
}

.child:nth-child(5n) {
  background: pink;
  grid-column: span 1;
  grid-row: span 2;
}

<!-- language: lang-html -->

&lt;div class=&quot;parent&quot;&gt;
  &lt;div class=&quot;child&quot;&gt;child-1&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-2&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-3&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-4&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-5&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-6&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-7&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-8&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-9&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-10&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-11&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-12&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-13&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-14&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-15&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-16&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-17&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-18&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-19&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-20&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-21&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-22&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-23&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-24&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-25&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-26&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-27&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-28&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-29&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-30&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-31&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-32&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Any help would be appreciated.

答案1

得分: 3

你可以通过指定一个包含4列的网格来实现这个。

编辑:描述已更改,图片使所需内容更清晰。以下是编辑后的版本:

...然后注意到除了 :nth-child(20n+5), :nth-child(20n+6), :nth-child(20n+19) 或 :nth-child(20n+20) 之外,所有内容都在单个单元格中,对于这些,将列跨度设置为2。

以下是一个简单的片段,添加了一些额外元素以确保我们在重复模式:

<style>
  .parent {
    display: grid;
    width: 100vw;
    grid-template-columns: repeat(4, 1fr);
  }
  
  .child:nth-child(20n + 5),
  .child:nth-child(20n + 6),
  .child:nth-child(20n + 19),
  .child:nth-child(20n + 20) {
    grid-column: span 2;
  }
</style>
<div class="parent">
  <div class="child">child-1</div>
  <div class="child">child-2</div>
  <div class="child">child-3</div>
  ...
  <div class="child">child-42</div>
</div>

完成片段。

英文:

You can do this by specifying a grid of 4 columns...

EDIT: the description changed and the image makes what is needed clearer. Here's edited version:

...then noticing that everything is in a single cell unless it is a :nth-child(20n+5), :nth-child(20n+6), :nth-child(20n+19) or :nth-child(20n+20) and for these setting the column span to 2.

Here's a simple snippet with a few more elements added to make certain we are repeating the pattern:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;style&gt;
  .parent {
    display: grid;
    width: 100vw;
    grid-template-columns: repeat(4, 1fr);
  }
  
  .child:nth-child(20n + 5),
  .child:nth-child(20n + 6),
  .child:nth-child(20n + 19),
  .child:nth-child(20n + 20) {
    grid-column: span 2;
  }
&lt;/style&gt;
&lt;div class=&quot;parent&quot;&gt;
  &lt;div class=&quot;child&quot;&gt;child-1&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-2&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-3&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-4&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-5&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-6&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-7&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-8&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-9&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-10&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-11&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-12&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-13&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-14&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-15&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-16&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-17&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-18&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-19&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-20&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-21&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-22&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-23&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-24&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-25&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-26&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-27&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-28&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-29&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-30&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-31&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-32&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-33&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-34&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-35&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-36&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-37&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-38&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-39&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-40&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-41&lt;/div&gt;
  &lt;div class=&quot;child&quot;&gt;child-42&lt;/div&gt;

&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 3

你需要选择第5、第6、第19和第20个每20个子元素。 :nth-child() 接受 <code><b>a</b>n+<b>b</b></code> 格式的公式,在其中我们需要将 a 替换为20,b 替换为5/6/19/20:

.child:nth-child(20n + 5),
.child:nth-child(20n + 6),
.child:nth-child(20n + 19),
.child:nth-child(20n + 20) {}

试试看:

<!-- begin snippet: js hide: true console: false babel: false -->
<!-- language: lang-js -->
const parent = document.querySelector('.parent');

for (let i = 0, l = 40 + Math.random() * 40 | 0; i < l; i++) {
  const child = document.createElement('div');
  child.classList.add('child');
  child.textContent = `child-${i + 1}`;
  parent.appendChild(child);
}

<!-- language: lang-css -->
.parent {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
}

.child {
  background: tomato;
  height: 20px;
}

.child:nth-child(20n + 5),
.child:nth-child(20n + 6),
.child:nth-child(20n + 19),
.child:nth-child(20n + 20) {
  background: cadetblue;
  grid-column: span 2;
}

<!-- language: lang-html -->
<div class="parent"></div>

<!-- end snippet -->
英文:

You need to select the 5th, 6th, 19th and 20th every 20 children. :nth-child() accepts a formula of <code><b>a</b>n+<b>b</b></code> form, in which we need to replace a with 20 and b with 5/6/19/20:

.child:nth-child(20n + 5),
.child:nth-child(20n + 6),
.child:nth-child(20n + 19),
.child:nth-child(20n + 20) {}

Try it:

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->

const parent = document.querySelector(&#39;.parent&#39;);

for (let i = 0, l = 40 + Math.random() * 40 | 0; i &lt; l; i++) {
  const child = document.createElement(&#39;div&#39;);
  child.classList.add(&#39;child&#39;);
  child.textContent = `child-${i + 1}`;
  parent.appendChild(child);
}

<!-- language: lang-css -->

.parent {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
}

.child {
  background: tomato;
  height: 20px;
}

.child:nth-child(20n + 5),
.child:nth-child(20n + 6),
.child:nth-child(20n + 19),
.child:nth-child(20n + 20) {
  background: cadetblue;
  grid-column: span 2;
}

<!-- language: lang-html -->

&lt;div class=&quot;parent&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 1

你们两个离解决方案真的很接近。根据图像,应该加宽的单元格是第五个和第六个,然后再加宽14个单元格(前面12个是两个较宽的单元格)。

这段代码会实现这个效果:
```css
.child:nth-child(14n + 5), .child:nth-child(14n + 6) {
    grid-column: span 2;
}

并且应该移除原始代码中的这部分:

.child:nth-child(-n + 2) {
  grid-column: span 2; 
  grid-row: span 2;
}

.child:nth-child(5n) {
  grid-column: span 1; 
  grid-row: span 2;
}

更新
为了匹配评论中提到的模式(4->2->12->2->4->2->12->2),可以使用以下代码:

.child:nth-child(20n + 5), .child:nth-child(20n + 6), .child:nth-child(20n + 19), .child:nth-child(20n + 20)  {
    grid-column: span 2;
}
英文:

You are both really close to the solution. According to the image the cells that should be wider are the 5th and 6th ones, and then again 14 cells later (12 + the 2 wider ones).

This code will do that:

.child:nth-child(14n + 5), .child:nth-child(14n + 6) {
    grid-column: span 2;
}

And this segment from the original code should be removed:

.child:nth-child(-n + 2) {
  grid-column: span 2; 
  grid-row: span 2;
}

.child:nth-child(5n) {
  grid-column: span 1; 
  grid-row: span 2;
}

Update
To match the pattern (4->2->12->2->4->2->12->2) mentioned in the comment this code will work:

.child:nth-child(20n + 5), .child:nth-child(20n + 6), .child:nth-child(20n + 19), .child:nth-child(20n + 20)  {
    grid-column: span 2;
}

huangapple
  • 本文由 发表于 2023年6月22日 15:51:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定