如何摆脱网格项底部的额外空格?

问题描述 投票:2回答:2

我的CSS Grid项目底部有很多空白区域,我想找出原因。我将图像设置为容器的40%高度,其高度设置为100%高度。我已经理解,默认情况下,网格项的高度(连续)是其内容的高度。隐式行中的所有项目如果只跨越一行,则应具有相同的高度,这就是我的情况。

如何让图像下方的部分仅占用下一个和任何填充所需的空间,而不是更多?

.cards {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: min-content;
  grid-gap: 1rem;

}

.card {
  height: 100%;
  border: 1px solid black;
}

.main-img {
  display: block;
  object-fit: cover;
  height: 40%;
  width: 100%;
}

.author-img {
  display: none;
}
<body>
    <div class="cards">
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1554142918-2c055786cf67?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
          <div class="card-content">
            <h4>Blogging</h4>
            <h2>The challenges & rewards of being vulnerable</h2>
            <div class="metadata">
              <img class="author-img" src="https://via.placeholder.com/1000x800/fb0" alt="">
              <div>
                <p class="author-name">By Alex</p>
                <p class="article-date">on April 16th, 2019</p>
              </div>
            </div>
          </div>
        </article>
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1553994535-aa6c2ee8cfc1?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
          <div class="card-content">
            <h4>Blogging</h4>
            <h2>Content strategy best practices vs reality with Alex of Scripted</h2>
            <div class="metadata">
              <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
              <div>
                <p class="author-name">By Alex</p>
                <p class="article-date">on April 16th, 2019</p>
              </div>
            </div>
          </div>
        </article>
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1554283048-23c1133c646e?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
          <div class="card-content"><h4>Blogging</h4>
          <h2>The selling 7: How to make amazing employee bio videos (+Examples)</h2>
          <div class="metadata">
            <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
            <div>
              <p class="author-name">By Alex</p>
              <p class="article-date">on April 16th, 2019</p>
            </div>
          </div></div>
        </article>
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1554302006-5c11782b1f69?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt=""> 
          <div class="card-content">
            <h4>Blogging</h4>
            <h2>The challenges & rewards of being vulnerable</h2>
            <div class="metadata">
              <img class="author-img" src="https://via.placeholder.com/1000x800/fb0" alt="">
              <div>
                <p class="author-name">By Alex</p>
                <p class="article-date">on April 16th, 2019</p>
              </div>
            </div>
          </div>
        </article>
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1553586633-929ec02f4fb4?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
          <div class="card-content">
            <h4>Blogging</h4>
            <h2>Content strategy best practices vs reality with Alex Barron of Scripted</h2>
            <div class="metadata">
              <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
              <div>
                <p class="author-name">By Alex</p>
                <p class="article-date">on April 16th, 2019</p>
              </div>
            </div>
          </div>
        </article>
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1548247416-ec66f4900b2e?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
          <div class="card-content">
            <h4>Blogging</h4>
            <h2>The selling 7: How to make amazing employee bio videos (+Examples)</h2>
            <div class="metadata">
              <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
              <div>
                <p class="author-name">By Alex</p>
                <p class="article-date">on April 16th, 2019</p>
              </div>
            </div>
          </div>
        </article>
    </div>
</body>

CodePen

html css css3 css-grid
2个回答
1
投票

您需要在px or rem or em中设置40%的高度,因为40%的计算方式不同,为了响应您可以使用% or vh但在px or rem or em中设置的高度更适合this案例。

.cards {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: min-content;
  grid-gap: 1rem;
}

.card {
  border: 1px solid black;
}

.main-img {
  display: block;
  object-fit: cover;
  height: 15rem;
  width: 100%;
}

.author-img {
  display: none;
}
<body>
    <div class="cards">
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1554142918-2c055786cf67?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
          <div class="card-content">
            <h4>Blogging</h4>
            <h2>The challenges & rewards of being vulnerable</h2>
            <div class="metadata">
              <img class="author-img" src="https://via.placeholder.com/1000x800/fb0" alt="">
              <div>
                <p class="author-name">By Alex</p>
                <p class="article-date">on April 16th, 2019</p>
              </div>
            </div>
          </div>
        </article>
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1553994535-aa6c2ee8cfc1?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
          <div class="card-content">
            <h4>Blogging</h4>
            <h2>Content strategy best practices vs reality with Alex of Scripted</h2>
            <div class="metadata">
              <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
              <div>
                <p class="author-name">By Alex</p>
                <p class="article-date">on April 16th, 2019</p>
              </div>
            </div>
          </div>
        </article>
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1554283048-23c1133c646e?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
          <div class="card-content"><h4>Blogging</h4>
          <h2>The selling 7: How to make amazing employee bio videos (+Examples)</h2>
          <div class="metadata">
            <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
            <div>
              <p class="author-name">By Alex</p>
              <p class="article-date">on April 16th, 2019</p>
            </div>
          </div></div>
        </article>
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1554302006-5c11782b1f69?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt=""> 
          <div class="card-content">
            <h4>Blogging</h4>
            <h2>The challenges & rewards of being vulnerable</h2>
            <div class="metadata">
              <img class="author-img" src="https://via.placeholder.com/1000x800/fb0" alt="">
              <div>
                <p class="author-name">By Alex</p>
                <p class="article-date">on April 16th, 2019</p>
              </div>
            </div>
          </div>
        </article>
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1553586633-929ec02f4fb4?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
          <div class="card-content">
            <h4>Blogging</h4>
            <h2>Content strategy best practices vs reality with Alex Barron of Scripted</h2>
            <div class="metadata">
              <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
              <div>
                <p class="author-name">By Alex</p>
                <p class="article-date">on April 16th, 2019</p>
              </div>
            </div>
          </div>
        </article>
        <article class="card">
          <img class="main-img" src="https://images.unsplash.com/photo-1548247416-ec66f4900b2e?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
          <div class="card-content">
            <h4>Blogging</h4>
            <h2>The selling 7: How to make amazing employee bio videos (+Examples)</h2>
            <div class="metadata">
              <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
              <div>
                <p class="author-name">By Alex</p>
                <p class="article-date">on April 16th, 2019</p>
              </div>
            </div>
          </div>
        </article>
    </div>
</body>

0
投票

如何让图像下方的部分仅占用下一个和任何填充所需的空间,而不是更多?

图像后面的网格项是一个内部带有标题(h4)的div。此标题具有由浏览器设置的默认顶部和底部边距。

enter image description here

enter image description here

使用h4 { margin-top: 0 }覆盖此默认值

.cards {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: min-content;
  grid-gap: 1rem;
}

.card {
  height: 100%;
  border: 1px solid black;
}

.main-img {
  display: block;
  object-fit: cover;
  height: 40%;
  width: 100%;
}

.author-img {
  display: none;
}

h4 {
  margin-top: 0;
}
<div class="cards">
  <article class="card">
    <img class="main-img" src="https://images.unsplash.com/photo-1554142918-2c055786cf67?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
    <!--           <h1>Alex</h1> -->
    <div class="card-content">
      <h4>Blogging</h4>
      <h2>The challenges & rewards of being vulnerable</h2>
      <div class="metadata">
        <img class="author-img" src="https://via.placeholder.com/1000x800/fb0" alt="">
        <div>
          <p class="author-name">By Alex</p>
          <p class="article-date">on April 16th, 2019</p>
        </div>
      </div>
    </div>
  </article>
  <article class="card">
    <img class="main-img" src="https://images.unsplash.com/photo-1553994535-aa6c2ee8cfc1?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
    <div class="card-content">
      <h4>Blogging</h4>
      <h2>Content strategy best practices vs reality with Alex of Scripted</h2>
      <div class="metadata">
        <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
        <div>
          <p class="author-name">By Alex</p>
          <p class="article-date">on April 16th, 2019</p>
        </div>
      </div>
    </div>
  </article>
  <article class="card">
    <img class="main-img" src="https://images.unsplash.com/photo-1554283048-23c1133c646e?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
    <div class="card-content">
      <h4>Blogging</h4>
      <h2>The selling 7: How to make amazing employee bio videos (+Examples)</h2>
      <div class="metadata">
        <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
        <div>
          <p class="author-name">By Alex</p>
          <p class="article-date">on April 16th, 2019</p>
        </div>
      </div>
    </div>
  </article>
  <article class="card">
    <img class="main-img" src="https://images.unsplash.com/photo-1554302006-5c11782b1f69?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
    <div class="card-content">
      <h4>Blogging</h4>
      <h2>The challenges & rewards of being vulnerable</h2>
      <div class="metadata">
        <img class="author-img" src="https://via.placeholder.com/1000x800/fb0" alt="">
        <div>
          <p class="author-name">By Alex</p>
          <p class="article-date">on April 16th, 2019</p>
        </div>
      </div>
    </div>
  </article>
  <article class="card">
    <img class="main-img" src="https://images.unsplash.com/photo-1553586633-929ec02f4fb4?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
    <div class="card-content">
      <h4>Blogging</h4>
      <h2>Content strategy best practices vs reality with Alex Barron of Scripted</h2>
      <div class="metadata">
        <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
        <div>
          <p class="author-name">By Alex</p>
          <p class="article-date">on April 16th, 2019</p>
        </div>
      </div>
    </div>
  </article>
  <article class="card">
    <img class="main-img" src="https://images.unsplash.com/photo-1548247416-ec66f4900b2e?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ" alt="">
    <div class="card-content">
      <h4>Blogging</h4>
      <h2>The selling 7: How to make amazing employee bio videos (+Examples)</h2>
      <div class="metadata">
        <img class="author-img" src="https://via.placeholder.com/1000x800/cf0" alt="">
        <div>
          <p class="author-name">By Alex</p>
          <p class="article-date">on April 16th, 2019</p>
        </div>
      </div>
    </div>
  </article>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.