【问题标题】:With CSS grid, how do I prevent other columns from streching when one column's height gets larger?使用 CSS 网格,当一列的高度变大时,如何防止其他列拉伸?
【发布时间】:2020-10-30 13:00:43
【问题描述】:

我有这样的布局:(HTML/CSS 在问题的底部)

现在,当网格项目“一”的高度变大时,项目“二”、“三”和“四”被拉伸,如下所示:

但我想要的是让他们一起保持在中心,而不是伸展,就像这样:

如何使用 CSS Grid 实现这一点?这是 HTML 和 CSS:

.my-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  grid-template-rows: auto auto auto;
  grid-template-areas: "one two" "one three" "one four";
  gap: 15px;
}
.one {
  grid-area: one;
  height: 300px;
}

.two {
  grid-area: two;
}

.three {
  grid-area: three;
}

.four {
  grid-area: four;
}

/* Just for aesthetics! */
body {
  background-color: #e9e9e9;
  margin: 0;
  padding: 20px;
}
.card {
  background-color: white;
  padding: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 30px;
}
<div class="my-grid">
    <div class="card one">One</div>
    <div class="card two">Two</div>
    <div class="card three">Three</div>
    <div class="card four">Four</div>
</div>

【问题讨论】:

    标签: html css sass css-grid


    【解决方案1】:

    添加另外两行。在顶部和底部。还要用底部边距替换行间距,以避免顶部/底部出现空白

    .my-grid {
      display: grid;
      grid-template-columns: 2fr 1fr;
      grid-template-rows:1fr repeat(3,auto) 1fr;
      column-gap: 15px;
    }
    .one {
      grid-row:1 / span 5;
      height: 300px;
    }
    
    .two {
      grid-row: 2;
      margin-bottom:15px;
    }
    .three {
      grid-row: 3;
      margin-bottom:15px;
    }
    .four {
      grid-row: 4;
    }
    
    /* Just for aesthetics! */
    body {
      background-color: #e9e9e9;
      margin: 0;
      padding: 20px;
    }
    .card {
      background-color: white;
      padding: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 30px;
    }
    <div class="my-grid">
        <div class="card one">One</div>
        <div class="card two">Two</div>
        <div class="card three">Three</div>
        <div class="card four">Four</div>
    </div>

    另一个可以玩对齐的想法

    .my-grid {
      display: grid;
      grid-template-columns: 2fr 1fr;
      grid-template-rows: 1fr auto 1fr;
      gap: 15px;
    }
    .one {
      grid-row: span 3;
      height: 300px;
    }
    .two {
      margin-top:auto;
    }
    .four {
      margin-bottom:auto;
    }
    
    
    /* Just for aesthetics! */
    body {
      background-color: #e9e9e9;
      margin: 0;
      padding: 20px;
    }
    .card {
      background-color: white;
      padding: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 30px;
    }
    <div class="my-grid">
        <div class="card one">One</div>
        <div class="card two">Two</div>
        <div class="card three">Three</div>
        <div class="card four">Four</div>
    </div>

    【讨论】:

    • 谢谢,这太棒了。但难道没有一种不那么老套的方法来实现这一点吗? min-content 之类的东西在这种情况下不起作用,对吧?我们没有办法避免额外的 2 行吗?
    • @Arad 添加了第二个想法
    • 不错!我喜欢这一个。但是顺便说一句,我们可以用align-self: end;代替margin-top: auto,对吧?
    • @Arad 是的,它应该给出相同的结果
    • @Arad 那么您需要使用的不是 CSS 网格,而是 flexbox。您无法使用 CSS 网格实现所需的居中
    【解决方案2】:

    您还可以在模板区域中添加额外的行。

    .my-grid {
      display: grid;
      grid-template-columns: 2fr 1fr;
      grid-template-rows: auto auto auto;
      grid-template-areas: "one .." "one two" "one three" "one four" "one ..";
      gap: 15px;
    }
    .one {
      grid-area: one;
      height: 300px;
    }
    
    .two {
      grid-area: two;
    }
    
    .three {
      grid-area: three;
    }
    
    .four {
      grid-area: four;
    }
    
    /* Just for aesthetics! */
    body {
      background-color: #e9e9e9;
      margin: 0;
      padding: 20px;
    }
    .card {
      background-color: white;
      padding: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 30px;
    }
    <div class="my-grid">
        <div class="card one">One</div>
        <div class="card two">Two</div>
        <div class="card three">Three</div>
        <div class="card four">Four</div>
    </div>

    或使用边距以避免多余的行:

    .my-grid {
      display: grid;
      grid-template-columns: 2fr 1fr;
      grid-template-rows: 1fr auto 1fr;
      grid-template-areas:   "one two" "one three" "one four"  ;
      gap: 15px;
    }
    .one {
      grid-area: one;
      height: 300px;
    }
    
    .two {
      grid-area: two;
      margin-top:auto;
    }
    
    .three {
      grid-area: three;
    }
    
    .four {
      grid-area: four;
      margin-bottom:auto;
    }
    
    /* Just for aesthetics! */
    body {
      background-color: #e9e9e9;
      margin: 0;
      padding: 20px;
    }
    .card {
      background-color: white;
      padding: 10px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 30px;
    }
    <div class="my-grid">
        <div class="card one">One</div>
        <div class="card two">Two</div>
        <div class="card three">Three</div>
        <div class="card four">Four</div>
    </div>

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 2010-11-14
      • 2015-11-02
      相关资源
      最近更新 更多