html - 网格布局模式的第 n 个子选择器

标签 html css design-patterns css-selectors grid

我正在尝试使用 css nth-child 解决不同的网格模式(如果可能的话,不用 JS)。

我正在尝试创建此模式以在页面下方重复:

repeating grid

目前我能达到的最好结果是前 3 个宽度为 33%,然后从那里开始的所有宽度为 25%

.service {
    width: 33%;
    &:nth-child(n+4),
    &:nth-child(n+5),
    &:nth-child(n+6),
    &:nth-child(n+7), {
        width: 25%;
    }
}

最佳答案

分别定位并重复前七项中的每一项。

article {
  display: flex;
  flex-wrap: wrap;
}
section {
  flex-grow: 1;
  height: 50px;  
  margin: 5px;
  background-color: black
}
section:nth-child(7n + 1) { flex-basis: 30%; }
section:nth-child(7n + 2) { flex-basis: 30%; }
section:nth-child(7n + 3) { flex-basis: 30%; }
section:nth-child(7n + 4) { flex-basis: 20%; }
section:nth-child(7n + 5) { flex-basis: 20%; }
section:nth-child(7n + 6) { flex-basis: 20%; }
section:nth-child(7n + 7) { flex-basis: 20%; }
<article>
  <section></section>
  <section></section>
  <section></section>  
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>  
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>
  <section></section>  
  <section></section>
  <section></section>
  <section></section>  
  <section></section>
  <section></section>  
  <section></section>   
</article>

https://jsfiddle.net/fy5cbgnr/

关于html - 网格布局模式的第 n 个子选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48308439/

相关文章:

javascript - 如何放置变量而不是文字数字?

javascript - 使用手势的 Android Web 应用程序图片库

css - 悬停另一个div时如何旋转图像?

javascript - 试图让元素在点击后出现一次,但出现不止一次

javascript - 简化 React 中的重复代码

model-view-controller - MVC 的替代方案

html - 放大时元素变大

javascript - 如何使用 JavaScript 对从 JSON 数据生成的 HTML Div 使用搜索/过滤器?

javascript - 更改圆环图的颜色

c++ - C++代码设计中的多代理系统