【问题标题】:CSS grid template columns - how does it work?CSS 网格模板列 - 它是如何工作的?
【发布时间】:2020-02-05 23:20:06
【问题描述】:

我是 CSS Grid 的新手,正在尝试了解它的工作原理。我找到了一个我一直在玩的布局,但我似乎无法弄清楚如何让文章跨越页脚/页眉的整个宽度。我已经尝试更改网格模板列,但是非常接近跨越它,但不是一直。

任何提示将不胜感激!

.grid {
  display: grid;
  grid-template-columns: 150px auto 0px;
  grid-gap: 1em;
}

header,
footer {
  grid-column: 1 / 4;
}

@media all and (max-width: 800px) {
  aside,
  article {
    grid-column: 1 / 4;
  }
}


/* Demo Specific Styles */

body {
  margin: 0 auto;
  max-width: 56em;
  padding: 1em 0;
}

header,
aside,
article,
footer {
  background: #eaeaea;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 25vh;
}

header {
  height: 250px;
}
<div class="grid">
  <header>
    Header
  </header>

  <aside class="sidebar-left">
    Left Sidebar
  </aside>

  <article>
    Article
  </article>

  <footer>
    Footer
  </footer>
</div>

【问题讨论】:

    标签: css css-grid


    【解决方案1】:

    我似乎无法弄清楚如何让文章跨越页脚/页眉的整个宽度。

    您已经创建了一个 3 列的网格:

    grid-template-columns: 150px auto 0px
    

    您已将页眉和页脚设置为跨越所有三列:

    header, footer {
      grid-column: 1 / 4;
    }
    

    但是,文章没有这样的说明。它只是扩展到第二列,因为这是默认设置(即grid-column { 2 / auto })。

    因此,您需要告诉文章扩展到第三列。将此添加到您的代码中:

    article {
       grid-column: 2 / 4;
    }
    

    .grid {
      display: grid;
      grid-template-columns: 150px auto 0px;
      grid-gap: 1em;
    }
    
    header,
    footer {
      grid-column: 1 / 4;
    }
    
    
    /* new */
    article {
      grid-column: 2 / 4;
    }
    
    @media all and (max-width: 800px) {
      aside,
      article {
        grid-column: 1 / 4;
      }
    }
    
    
    /* Demo Specific Styles */
    body {
      margin: 0 auto;
      max-width: 56em;
      padding: 1em 0;
    }
    
    header,
    aside,
    article,
    footer {
      background: #eaeaea;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 25vh;
    }
    
    header {
      height: 250px;
    }
    <div class="grid">
      <header>Header</header>
      <aside class="sidebar-left">Left Sidebar</aside>
      <article>Article</article>
      <footer>Footer</footer>
    </div>

    【讨论】:

    • 太棒了,谢谢你把它分解成这样!感谢帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2021-05-20
    • 2021-08-09
    • 1970-01-01
    • 2021-12-28
    • 2020-08-18
    相关资源
    最近更新 更多