【问题标题】:Grid-row-gap not applying percentage网格行间隙不应用百分比
【发布时间】:2018-05-16 22:07:23
【问题描述】:

所以我有一个 4x5 的图像网格,其中图像可以是各种尺寸,但最大为 180x180 像素。

<div class="grid">
    <div class="box 1 a"><img src="./assets/Account-Attempt-4-1.gif"></div>
     ...
    <div class="box 5 d"><img src="./assets/really-queer-Christmas1.gif"></div>
</div>

我的 CSS 会将我的列 2.5% 分开,但让我的行保持接触。这是因为我以像素为单位定义了行高吗?

.grid {
  margin-left: 5%;
  margin-right: 5%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 180px 180px 180px 180px 180px;
  grid-gap: 2.5% 2.5%;
}

.box {
  display: flex;
  align-items: center;
  justify-content: center;
}

【问题讨论】:

    标签: html css css-grid


    【解决方案1】:

    如果您想为您的grid-row-gap 使用百分比,您需要为您的.grid div 设置一个固定高度。否则你需要使用px。 此处示例:https://jsfiddle.net/t53s25us/

    .grid {
      margin-left: 5%;
      margin-right: 5%;
      display: grid;
      grid-template-columns: 1fr 1fr 1fr 1fr;
      grid-gap: 2.5% 2.5%;
      height: 200px;
    }
    
    .box {
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: red;
    }
    <div class="grid">
      <div class="box 1 a">AAAA</div>
      <div class="box 1 a">AAAA</div>
      <div class="box 1 a">AAAA</div>
      <div class="box 1 a">AAAA</div>
      <div class="box 1 a">AAAA</div>
    </div>

    【讨论】:

    • 好的,有道理,谢谢。我忘记了百分比大小使用父级的尺寸。
    猜你喜欢
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多