如何动态重复grid-template-areas

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

How can I dynamically repeat grid-template-areas

问题

我在WordPress上有一个网站,其中有一个用户可以添加任意数量照片的部分。这个部分是用网格创建的。现在的问题是,默认情况下,该部分中有10张照片。但当用户添加更多照片时,网格会破裂。如何使网格重复?

.grid {
  display: grid;
  grid-template-rows: auto;
  grid-template-columns: repeat(5, 1fr);
  grid-gap: 30px;
  grid-template-areas: "one two three four five" "six six seven seven eight" "nine ten seven seven eight";
}

.grid .grid-item:nth-child(1) {
  grid-area: one;
}

.grid .grid-item:nth-child(2) {
  grid-area: two;
}

.grid .grid-item:nth-child(3) {
  grid-area: three;
}

.grid .grid-item:nth-child(4) {
  grid-area: four;
}

.grid .grid-item:nth-child(5) {
  grid-area: five;
}

.grid .grid-item:nth-child(6) {
  grid-area: six;
}

.grid .grid-item:nth-child(7) {
  grid-area: seven;
}

.grid .grid-item:nth-child(8) {
  grid-area: eight;
}

.grid .grid-item:nth-child(9) {
  grid-area: nine;
}

.grid .grid-item:nth-child(10) {
  grid-area: ten;
}
<div class="grid">
  <div class="grid-item">GridItem1</div>
  <div class="grid-item">GridItem2</div>
  <div class="grid-item">GridItem3</div>
  <div class="grid-item">GridItem4</div>
  <div class="grid-item">GridItem5</div>
  <div class="grid-item">GridItem6</div>
  <div class="grid-item">GridItem7</div>
  <div class="grid-item">GridItem8</div>
  <div class="grid-item">GridItem9</div>
  <div class="grid-item">GridItem10</div>
</div>
英文:

I have a site on WordPress and a section where the user adds any number of photos. The section is made with grid. Now the problem is that by default there are 10 photos in the section. But when the user adds more, the grid breaks. How can I make the grid repeat?

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

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

.grid {
  display: grid;
  grid-template-rows: auto;
  grid-template-columns: repeat(5, 1fr);
  grid-gap: 30px;
  grid-template-areas: &quot;one two three four five&quot; &quot;six six seven seven eight&quot; &quot;nine ten seven seven eight&quot;;
}

.grid .grid-item:nth-child(1) {
  grid-area: one;
}

.grid .grid-item:nth-child(2) {
  grid-area: two;
}

.grid .grid-item:nth-child(3) {
  grid-area: three;
}

.grid .grid-item:nth-child(4) {
  grid-area: four;
}

.grid .grid-item:nth-child(5) {
  grid-area: five;
}

.grid .grid-item:nth-child(6) {
  grid-area: six;
}

.grid .grid-item:nth-child(7) {
  grid-area: seven;
}

.grid .grid-item:nth-child(8) {
  grid-area: eight;
}

.grid .grid-item:nth-child(9) {
  grid-area: nine;
}

.grid .grid-item:nth-child(10) {
  grid-area: ten;
}

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

&lt;div class=&quot;grid&quot;&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem1&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem2&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem3&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem4&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem5&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem6&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem7&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem8&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem9&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem10&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 3

不要使用网格区域,如果你想要重复的行为。只需定义列数或行数。我写了一篇详细的文章介绍这种技术:https://css-tricks.com/exploring-css-grids-implicit-grid-and-auto-placement-powers/

.grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-gap: 30px;
}
.grid > * {
  border: 1px solid red;
}
.grid .grid-item:nth-child(10n + 6) {
  grid-column: 1/span 2;
}
.grid .grid-item:nth-child(10n + 7) {
  grid-column: 3/span 2;
}
.grid .grid-item:nth-child(10n + 7),
.grid .grid-item:nth-child(10n + 8) {
  grid-row: span 2;
}
<div class="grid">
  <div class="grid-item">GridItem 1</div>
  <div class="grid-item">GridItem 2</div>
  <div class="grid-item">GridItem 3</div>
  <div class="grid-item">GridItem 4</div>
  <div class="grid-item">GridItem 5</div>
  <div class="grid-item">GridItem 6</div>
  <div class="grid-item">GridItem 7</div>
  <div class="grid-item">GridItem 8</div>
  <div class="grid-item">GridItem 9</div>
  <div class="grid-item">GridItem 10</div>
  <div class="grid-item">GridItem 11</div>
  <div class="grid-item">GridItem 12</div>
  <div class="grid-item">GridItem 13</div>
  <div class="grid-item">GridItem 14</div>
  <div class="grid-item">GridItem 15</div>
  <div class="grid-item">GridItem 16</div>
  <div class="grid-item">GridItem 17</div>
  <div class="grid-item">GridItem 18</div>
  <div class="grid-item">GridItem 19</div>
  <div class="grid-item">GridItem 20</div>
</div>
英文:

Don't use grid areas if you want to have a repeating behavior. Simply define the number of columns or rows. I wrote a detailed article about such technique: https://css-tricks.com/exploring-css-grids-implicit-grid-and-auto-placement-powers/

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

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

.grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-gap: 30px;
}
.grid &gt; * {
  border: 1px solid red;
}
.grid .grid-item:nth-child(10n + 6) {
  grid-column: 1/span 2;
}
.grid .grid-item:nth-child(10n + 7) {
  grid-column: 3/span 2;
}
.grid .grid-item:nth-child(10n + 7),
.grid .grid-item:nth-child(10n + 8) {
  grid-row: span 2;
}

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

&lt;div class=&quot;grid&quot;&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 1&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 2&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 3&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 4&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 5&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 6&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 7&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 8&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 9&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 10&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 11&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 12&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 13&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 14&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 15&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 16&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 17&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 18&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 19&lt;/div&gt;
  &lt;div class=&quot;grid-item&quot;&gt;GridItem 20&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 17:03:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682402.html
匿名

发表评论

匿名网友

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

确定