“Css Grid Dynamic layouts” 可翻译为 “CSS 网格动态布局”。

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

Css Grid Dynamic layouts

问题

我正在尝试创建一个包含1-4个元素的动态网格布局。他们在 https://css-tricks.com/exploring-css-grids-implicit-grid-and-auto-placement-powers/ 的“dynamic layouts”部分实现了与我所需的类似的东西,但略有不同。

这个布局的CSS代码如下:

.grid {
  display: grid;
}
.grid :nth-child(2) {
  grid-column-start: 2;
}
.grid :nth-child(3):last-child {
  grid-column-start: span 2;
}

我试图做类似但不同的事情,但我无法实现。这是我想要实现的效果图片:

“Css Grid Dynamic layouts” 可翻译为 “CSS 网格动态布局”。

英文:

I am trying to make a dynamic grid layout containing 1-4 elements. They achieved something close to what I am looking for at https://css-tricks.com/exploring-css-grids-implicit-grid-and-auto-placement-powers/ under the "dynamic layouts" section. But slightly different.

“Css Grid Dynamic layouts” 可翻译为 “CSS 网格动态布局”。

The CSS for this layout:

.grid {
  display: grid;
}
.grid :nth-child(2) {
  grid-column-start: 2;
}
.grid :nth-child(3):last-child {
  grid-column-start: span 2;
}

I am trying to do something similar but different, but I cannot get it. This is a picture of what I want to achieve:

“Css Grid Dynamic layouts” 可翻译为 “CSS 网格动态布局”。

答案1

得分: 3

你可以使用CSS的nth-last-child选择器来识别第一个子元素是三个子元素中的第一个。

.grid {
  display: grid;
}

.grid :nth-child(2) {
  grid-column-start: 2;
}

.grid :nth-child(1):nth-last-child(3) {
  grid-row-start: span 2;
}

.grid {
  width: 300px;
  aspect-ratio: 1;
  grid-gap: 5px;
  outline: 2px solid red;
  vertical-align: top;
  margin: 10px;
  counter-reset: num;
}

.grid * {
  border: 2px solid;
  font-size: 30px;
  box-sizing: border-box;
  font-family: sans-serif;
  display: grid;
  place-content: center;
}

.grid *:before {
  content: counter(num);
  counter-increment: num;
}
<div class="grid">
  <div></div>
</div>
<div class="grid">
  <div></div>
  <div></div>
</div>
<div class="grid">
  <div></div>
  <div></div>
  <div></div>
</div>
<div class="grid">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>
英文:

You can use the CSS nth-last-child selector to spot when the first child is the first of three children.

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

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

.grid {
  display: grid;
}

.grid :nth-child(2) {
  grid-column-start: 2;
}

.grid :nth-child(1):nth-last-child(3) {
  grid-row-start: span 2;
}


/**/

.grid {
  width: 300px;
  aspect-ratio: 1;
  grid-gap: 5px;
  outline: 2px solid red;
  vertical-align: top;
  margin: 10px;
  counter-reset: num;
}

.grid * {
  border: 2px solid;
  font-size: 30px;
  box-sizing: border-box;
  font-family: sans-serif;
  display: grid;
  place-content: center;
}

.grid *:before {
  content: counter(num);
  counter-increment: num;
}

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

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

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月6日 12:56:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027072.html
匿名

发表评论

匿名网友

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

确定