【问题标题】:Position grid items starting at the bottom从底部开始定位网格项目
【发布时间】:2018-08-27 11:06:48
【问题描述】:

我有一个 CSS 网格,在这个网格中的文章可能是博客文章。它们由左侧的图像和右侧的文本组成。

我需要文章从底部开始,以便较新的文章出现在它们上方。但我就是不能让他们从底部开始,无论我尝试什么都行不通。 align-items: end; 应该可以解决问题,但它没有……那么我在这里错过了什么?

.blog-Grid {
  display: grid;
}

.post {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  width: 50%;
  margin: 0 0 10% 15%;
}
<section class="blog-Grid">
  <article class="post">
    <img id="img" src="images/img1.jpeg" alt="">
    <div class="post-text-box">
      <h3 class="post-header"> I'm a header </h3>
      <p class="post-text"> Lorem ipsum text. </p>
    </div>
  </article>
  <article class="post">
    <img id="img" src="images/img2.jpg" alt="">
    <div class="post-text-box">
      <h3 class="post-header"> I'm a header </h3>
      <p class="post-text"> Lorem ipsum text.</p>
    </div>
  </article>
</section>

【问题讨论】:

  • 看到您在帖子中放置了grid-template columns,最终产品应该是什么的说明会使问题变得更好。理想情况下,这应该使用display: grid 附加到类以勾勒网格。

标签: html css css-grid


【解决方案1】:

您可以将 flexbox 与主网格一起使用,并将 CSS 网格仅用于帖子。

.blog-Grid {
  display: flex;
  min-height:200vh;
  flex-direction:column;
  justify-content:flex-end;
}

.post {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  width: 50%;
  margin: 0 0 10% 15%;
}
<section class="blog-Grid">

  <article class="post">
    <img id="img" src="https://lorempixel.com/400/200/" alt="">

    <div class="post-text-box">
      <h3 class="post-header"> I'm a header </h3>
      <p class="post-text"> Lorem ipsum text. </p>
    </div>

  </article>

  <article class="post">
    <img id="img" src="https://lorempixel.com/400/200/" alt="">

    <div class="post-text-box">
      <h3 class="post-header"> I'm a header </h3>
      <p class="post-text"> Lorem ipsum text.</p>
    </div>

  </article>


</section>

【讨论】:

    【解决方案2】:

    那么我在这里错过了什么?

    您忽略了 HTML 元素默认为 height: auto 的事实(请参阅下面的参考资料)。这意味着它们是其内容的高度。这意味着没有额外的空间用于垂直对齐。

    这是您的代码的简化版本。我在容器周围添加了一个边框。请注意高度是如何自动“缩小以适应”的。

    .blog-Grid {
      display: grid;
      border: 2px solid red;
    }
    
    .post {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
    }
    
    * {
      margin: 0;
      box-sizing: border-box;
    }
    <section class="blog-Grid">
      <article class="post">
        <img id="img" src="images/img1.jpeg" alt="">
        <div class="post-text-box">
          <h3 class="post-header"> I'm a header </h3>
          <p class="post-text"> Lorem ipsum text. </p>
        </div>
      </article>
      <article class="post">
        <img id="img" src="images/img2.jpg" alt="">
        <div class="post-text-box">
          <h3 class="post-header"> I'm a header </h3>
          <p class="post-text"> Lorem ipsum text.</p>
        </div>
      </article>
    </section>

    因此,非常简单,为您的容器添加高度以创建额外空间。

    .blog-Grid {
      display: grid;
      height: 100vh;
      align-content: end;
      border: 2px solid red;
    }
    
    .post {
      display: grid;
      grid-template-columns: auto 1fr;
      align-items: center;
    }
    
    * {
      margin: 0;
      box-sizing: border-box;
    }
    <section class="blog-Grid">
      <article class="post">
        <img src="http://i.imgur.com/60PVLis.png" width="100" height="100" alt="">
        <div class="post-text-box">
          <h3 class="post-header"> I'm a header </h3>
          <p class="post-text"> Lorem ipsum text. </p>
        </div>
      </article>
      <article class="post">
        <img src="http://i.imgur.com/60PVLis.png" width="100" height="100" alt="">
        <div class="post-text-box">
          <h3 class="post-header"> I'm a header </h3>
          <p class="post-text"> Lorem ipsum text.</p>
        </div>
      </article>
    </section>

    jsFiddle demo

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 2022-12-14
      • 1970-01-01
      • 2011-09-25
      相关资源
      最近更新 更多