【问题标题】:How can I get a 3 columns by 2 rows symmetrical layout with 5 elements using CSS grid?如何使用 CSS 网格获得 5 个元素的 3 列 2 行对称布局?
【发布时间】:2021-11-10 11:28:50
【问题描述】:

我已经使用CSS flexbox获得了下面的布局:

* {
  font-family: Arial, Helvetica, sans-serif;
  box-sizing: border-box;
}

.my-grid {
  list-style-type: none;
  padding: 0;
  margin: 0 auto;
  display: flex;
  width: 162px;
  flex-wrap: wrap;
}


.my-grid li {
  border: 1px solid rgba(0, 0, 0 , 0.125);
  background:#c00;
  color: #fff;
  width: 50px;
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 2px;
} 

.my-grid li:nth-child(4),
.my-grid li:nth-child(5) {
  width: 77px;
}
<ul class="my-grid">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

但是,我尝试使用 CSS 网格 获得相同结果的尝试失败了。我得到的只是:

* {
  font-family: Arial, Helvetica, sans-serif;
  box-sizing: border-box;
}

.my-grid {
  width: 160px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: repeat(2, 50px);
  grid-gap: 5px;
  list-style-type: none;
  margin: 0 auto;
  padding: 0;
}


.my-grid li {
  border: 1px solid rgba(0, 0, 0 , 0.125);
  background:#c00;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
} 
<ul class="my-grid">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

我希望能够通过使用 CSS 网格以尽可能少的行来实现您在第一个 sn-p 中看到的结果。 (我发现 CSS 网格通常非常灵活和优雅)。

这可能吗?我错过了什么?

【问题讨论】:

    标签: css flexbox css-grid


    【解决方案1】:

    只需告诉孩子们默认跨越 6 列网格中的 2 列,而较宽的则跨越 3 列..

    * {
      font-family: Arial, Helvetica, sans-serif;
      box-sizing: border-box;
    }
    
    .my-grid {
      width: 160px;
      display: grid;
      grid-template-columns: repeat(6, 1fr);
      grid-template-rows: repeat(2, 50px);
      grid-gap: 5px;
      list-style-type: none;
      margin: 0 auto;
      padding: 0;
    }
    
    .my-grid li {
      border: 1px solid rgba(0, 0, 0, 0.125);
      background: #c00;
      color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
      grid-column: span 2;
    }
    
    .my-grid li:nth-last-child(2),
    .my-grid li:nth-last-child(1) {
      grid-column: span 3
    }
    <ul class="my-grid">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>

    【讨论】:

    • 这是一个非常棒的解决方案。
    【解决方案2】:

    一种方式(更多行然后弯曲):

    * {
      font-family: Arial, Helvetica, sans-serif;
      box-sizing: border-box;
    }
    
    .my-grid {
      width: 160px;
      display: grid;
      grid-template-columns: repeat(6, 1fr);
      grid-template-rows: repeat(2, 50px);
      grid-gap: 5px;
      list-style-type: none;
      margin: 0 auto;
      padding: 0;
    }
    
    
    .my-grid li {
      border: 1px solid rgba(0, 0, 0 , 0.125);
      background:#c00;
      color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .my-grid li:first-child {
      grid-column: 1/3;
    }
    .my-grid li:nth-child(2) {
      grid-column: 3/5;
    }
    .my-grid li:nth-child(3) {
      grid-column: 5/7;
    }
    .my-grid li:nth-child(4) {
      grid-row: 2;
      grid-column: 1/4;
    }
    .my-grid li:last-child {
      grid-row: 2;
      grid-column: 4/7;
    }
    <ul class="my-grid">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 2022-12-01
      • 2019-05-21
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多