代码之家  ›  专栏  ›  技术社区  ›  Ben

动态创建具有n列等宽的网格

  •  0
  • Ben  · 技术社区  · 5 年前

    使用SCSS,如何动态创建 grid 已经有了 n 平等的

    当前方法:

    // grid setup
    $columns: 4;
    $columnGap: 20px;
    $rowGap: 30px;
    
    // grid
    display: grid;
    grid-template-columns: repeat( $columns, auto );
    grid-template-rows: auto;
    grid-column-gap: $columnGap;
    grid-row-gap: $rowGap;
    

    auto 不实际生成 相同的

    我希望所有的列都有完全相同的宽度。

    0 回复  |  直到 5 年前
        1
  •  3
  •   kukkuz    5 年前

    你可以用 grid-template-columns: repeat( $columns, 1fr) 香草色CSS

    :root {
      --columns: 4;
      --columnGap: 20px;
      --rowGap: 30px;
    }
    
    .grid {
      display: grid;
      grid-template-columns: repeat(var(--columns), 1fr);
      grid-template-rows: auto;
      grid-column-gap: var(--columnGap);
      grid-row-gap: var(--rowGap);
    }
    
    .grid>div {
      background: aliceblue;
    }
    <div class="grid">
      <div>Some text here</div>
      <div>Some </div>
      <div>Some text</div>
      <div>Some text here</div>
      <div>Some text here</div>
      <div></div>
      <div>Some </div>
      <div>Some</div>
      <div>Some text here</div>
      <div>Some text</div>
      <div>Some text here</div>
    </div>