【问题标题】:multiple widths css grid多宽度css网格
【发布时间】:2018-12-20 21:59:06
【问题描述】:

所以我正在尝试创建这样的网格

我的顶部和底部帖子大小相同,中间有一个帖子缩小并自动填充剩余空间。

这是我的代码

<section class="posts-grid">
  <article class="post">
    <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png?width=1464&name=pitchadri.png" alt="">

    <div class="blog-listing-info">
      <a href="#">
        <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
      </a>
      <p class="blog-listing-tag">Design - 11 min read</p>
    </div>
  </article>

  <article class="post">
    <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png?width=1464&name=pitchadri.png" alt="">

    <div class="blog-listing-info">
      <a href="#">
        <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
      </a>
      <p class="blog-listing-tag">Design - 11 min read</p>
    </div>
  </article>

  <article class="post long-post">
    <div class="blog-listing-info">
      <a href="#">
        <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
      </a>
      <p class="blog-listing-tag">Design - 11 min read</p>
    </div>
  </article>

  <article class="post">
    <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png?width=1464&name=pitchadri.png" alt="">

    <div class="blog-listing-info">
      <a href="#">
        <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
      </a>
      <p class="blog-listing-tag">Design - 11 min read</p>
    </div>
  </article>

  <article class="post">
    <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png?width=1464&name=pitchadri.png" alt="">

    <div class="blog-listing-info">
      <a href="#">
        <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
      </a>
      <p class="blog-listing-tag">Design - 11 min read</p>
    </div>
  </article>

  <article class="post">
    <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png?width=1464&name=pitchadri.png" alt="">

    <div class="blog-listing-info">
      <a href="#">
        <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
      </a>
      <p class="blog-listing-tag">Design - 11 min read</p>
    </div>
  </article>
</section>

css

.posts-grid {
    grid-template-areas:
    "post post"
    "long-post post"
    "post post";
    grid-template-columns: 15% 1fr;
    grid-template-rows: 80px 1fr;
    grid-auto-rows: 385px;
    grid-column-gap: 23px;
    grid-row-gap: 20px;
    height: 100vh;
    display: grid;
  }

  .post { grid-area: post; }
  .post.long-post { grid-area: long-post !important; }

我创建了一个代码笔 https://codepen.io/ben_bagley/pen/30272c1f61b4f20c080040f5359bd5f1,但正如您所见,它忽略了这样的好处

对于获得所需效果的任何帮助表示赞赏。

【问题讨论】:

  • 你可以交替使用不同的子网格,这里有另一个布局的想法:stackoverflow.com/questions/52898785/…
  • 将网格设置为 10 或 20 列,而不是两列。然后根据需要跨越网格区域。例如,左上角的项目可以跨越 5 列。左下方的项目可以跨越 10 个。
  • 你能提供一个例子吗@Michael_B 我是网格系统的新手
  • 实际上,正确的方法 (per the guidelines) 是让您在问题中发布足够的代码来重现问题。然后我们可以解决这个问题。
  • 我在这里创建了一个新问题@Michael_B stackoverflow.com/questions/53877736/…

标签: css css-grid


【解决方案1】:

这是我能做的最好的,我希望它有所帮助。

HTML:

<section class="posts-grid">
    <article class="post">
        <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png? 
         width=1464&name=pitchadri.png" alt="">

        <div class="blog-listing-info">
        <a href="#">
            <h2 class="blog-listing-title">13 things to make your US and UI design 
            much better</h2>
        </a>
        <p class="blog-listing-tag">Design - 11 min read</p>
        </div>
    </article>

    <article class="post">
        <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png? 
width=1464&name=pitchadri.png" alt="">

        <div class="blog-listing-info">
        <a href="#">
            <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
        </a>
        <p class="blog-listing-tag">Design - 11 min read</p>
        </div>
    </article>

    <article class="post">
        <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png?width=1464&name=pitchadri.png" alt="">

        <div class="blog-listing-info">
        <a href="#">
            <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
        </a>
        <p class="blog-listing-tag">Design - 11 min read</p>
        </div>
    </article>

    <article class="long-post">
        <div class="blog-listing-info">
        <a href="#">
            <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
        </a>
        <p class="blog-listing-tag">Design - 11 min read</p>
        </div>
    </article>

    <article class="post">
        <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png?width=1464&name=pitchadri.png" alt="">

        <div class="blog-listing-info">
        <a href="#">
            <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
        </a>
        <p class="blog-listing-tag">Design - 11 min read</p>
        </div>
    </article>

    <article class="post">
        <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png?width=1464&name=pitchadri.png" alt="">

        <div class="blog-listing-info">
        <a href="#">
            <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
        </a>
        <p class="blog-listing-tag">Design - 11 min read</p>
        </div>
    </article>

    <article class="post">
        <img src="https://blog.flyingsaucer.nyc/hs-fs/hubfs/blog/pitchadri.png?width=1464&name=pitchadri.png" alt="">

        <div class="blog-listing-info">
        <a href="#">
            <h2 class="blog-listing-title">13 things to make your US and UI design much better</h2>
        </a>
        <p class="blog-listing-tag">Design - 11 min read</p>
        </div>
    </article>
</section>

SCSS:

* {
    padding: 0px;
    margin: 0px;
    box-sizing: border-box;
}
   .posts-grid {
    padding: 10px;
    display: grid;
    grid-gap: 10px;
    grid-template-columns: repeat(5, 1fr);
    grid-template-rows: auto;
    .post, .long-post {
        padding: 10px;
        border-radius: 6px;
    }

    .post:first-child, .long-post + .post {
        background: greenyellow;
        grid-column-end: span 3;
    }
    .post + .post + .post {
        grid-column-end: span 1;
    }
    .post {
        background: lightblue;
        grid-column-start: auto;
        grid-column-end: span 2;
    }

    .long-post {
        background: red;
        grid-column-end: span 4;
    }

    img {
        width: 100%;
        height: auto;
    }
}

这里是 JsFiddle: https://jsfiddle.net/cisco336/ebt0zfvr/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2019-08-23
    • 2018-09-29
    相关资源
    最近更新 更多