css - CSS 网格中的垂直间距分布

标签 css css-grid

我是 CSS 网格新手,我遇到了布局问题: 我希望第一个条目上的段落文本位于标题正下方,但左侧跨越两行的图像同等地扩展右侧单元格,在标题和段落之间留下太多空间。

有没有办法确保第一行的大小仅适合标题的高度,并且段落行占据所有剩余空间?理想情况下,我想仅使用 css 来完成此操作。

通常我会使用标题和段落的容器 div 来执行此操作,但我使用 css 网格来更改移动布局中的源顺序,因此我认为容器不起作用。

Codepen link

body {
  background: #444;
}

.chunkheader,
.chunkpara,
.chunkimage {
  margin-top: 0;
  margin-bottom: 10px;
}

.chunk {
  background: #fff;
  padding: 20px;
  margin: 20px;
}

.chunkimage {
  max-width: 100%;
}

@media screen and (min-width:600px) {
  .chunk {
    display: grid;
    grid-template-columns: 200px auto;
    grid-column-gap: 20px;
  }
  .chunkimage {
    grid-column-start: 1;
    grid-row-start: 1;
    grid-row-end: 3;
  }
  .chunkheader {
    grid-column-start: 2;
  }
  .chunkpara {
    grid-column-start: 2;
  }
}
<div class="chunk">
  <h2 class="chunkheader">Entry one</h2>
  <img src="https://picsum.photos/600/700/?image=571" alt="" class="chunkimage">
  <p class="chunkpara">The A000005 is an Arduino Nanoboard which is a small, breadboard-friendly board based on the ATmega328 (Arduino Nano 3.0) or ATmega168 (Arduino Nano 2.x). It has similar functionality to the Arduino Duemilanove, but in a different package. It lacks
    only a DC power jack and is instead powered through the Mini-B USB connector. The Nano was designed and is being produced by Gravitech.</p>
</div>
<div class="chunk">
  <h2 class="chunkheader">Entry two</h2>
  <img src="https://picsum.photos/600/400/?image=572" alt="" class="chunkimage">
  <p class="chunkpara">The A000005 is an Arduino Nanoboard which is a small, breadboard-friendly board based on the ATmega328 (Arduino Nano 3.0) or ATmega168 (Arduino Nano 2.x). It has similar functionality to the Arduino Duemilanove, but in a different package. It lacks
    only a DC power jack and is instead powered through the Mini-B USB connector. The Nano was designed and is being produced by Gravitech.</p>
</div>
<div class="chunk">
  <h2 class="chunkheader">Entry three</h2>
  <img src="https://picsum.photos/600/400/?image=573" alt="" class="chunkimage">
  <p class="chunkpara">The A000005 is an Arduino Nanoboard which is a small, breadboard-friendly board based on the ATmega328 (Arduino Nano 3.0) or ATmega168 (Arduino Nano 2.x). It has similar functionality to the Arduino Duemilanove, but in a different package. It lacks
    only a DC power jack and is instead powered through the Mini-B USB connector. The Nano was designed and is being produced by Gravitech.</p>
</div>

它看起来如何: enter image description here

我希望它看起来如何: enter image description here

最佳答案

您可以指定一个grid-template-rows,如下所示:

body {
  background: #444;
}

.chunkheader,
.chunkpara,
.chunkimage {
  margin-top: 0;
  margin-bottom: 10px;
}

.chunk {
  background: #fff;
  padding: 20px;
  margin: 20px;
}

.chunkimage {
  max-width: 100%;
}

@media screen and (min-width:600px) {
  .chunk {
    display: grid;
    grid-template-columns: 200px auto;
    grid-template-rows:auto 1fr;
    grid-column-gap: 20px;
  }
  .chunkimage {
    grid-column-start: 1;
    grid-row-start: 1;
    grid-row-end: 3;
  }
  .chunkheader {
    grid-column-start: 2;
  }
  .chunkpara {
    grid-column-start: 2;
  }
}
<div class="chunk">
  <h2 class="chunkheader">Entry one</h2>
  <img src="https://picsum.photos/600/700/?image=571" alt="" class="chunkimage">
  <p class="chunkpara">The A000005 is an Arduino Nanoboard which is a small, breadboard-friendly board based on the ATmega328 (Arduino Nano 3.0) or ATmega168 (Arduino Nano 2.x). It has similar functionality to the Arduino Duemilanove, but in a different package. It lacks
    only a DC power jack and is instead powered through the Mini-B USB connector. The Nano was designed and is being produced by Gravitech.</p>
</div>

<div class="chunk">
  <h2 class="chunkheader">Entry two</h2>
  <img src="https://picsum.photos/600/400/?image=572" alt="" class="chunkimage">
  <p class="chunkpara">The A000005 is an Arduino Nanoboard which is a small, breadboard-friendly board based on the ATmega328 (Arduino Nano 3.0) or ATmega168 (Arduino Nano 2.x). It has similar functionality to the Arduino Duemilanove, but in a different package. It lacks
    only a DC power jack and is instead powered through the Mini-B USB connector. The Nano was designed and is being produced by Gravitech.</p>
</div>

<div class="chunk">
  <h2 class="chunkheader">Entry three</h2>
  <img src="https://picsum.photos/600/400/?image=573" alt="" class="chunkimage">
  <p class="chunkpara">The A000005 is an Arduino Nanoboard which is a small, breadboard-friendly board based on the ATmega328 (Arduino Nano 3.0) or ATmega168 (Arduino Nano 2.x). It has similar functionality to the Arduino Duemilanove, but in a different package. It lacks
    only a DC power jack and is instead powered through the Mini-B USB connector. The Nano was designed and is being produced by Gravitech.</p>
</div>

关于css - CSS 网格中的垂直间距分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52928137/

相关文章:

html - 具有不同列宽的响应式 flexbox 网格

html - 跨分辨率平台的字体大小百分比类似于 14px

html - 无法让边框出现在我的表格行的底部

css - 使用 pull Bootstrap 元素顺序

css - 当 minmax 为 : minmax(auto, auto 时会发生什么)

jquery - 选择html5中的选项选择(jquery)

html - 子项仅在 Firefox 中脱离内联网格父项

javascript - 在 JavaScript 中使用 CSS Grid repeat() 声明

html - 如何突出显示 CSS 网格单元格?

html - 你如何使 CSS 网格中的链接/href 显示为内联而不是 block ?