【发布时间】:2018-08-15 04:18:43
【问题描述】:
最近,我使用CSS grid 创建了一个布局。虽然这很好用,但我对它的工作原理感到困惑。具体来说,我对grid-template-rows: auto auto 1fr auto; 这一行感到困惑。
这里最终发生的是页眉和页脚根据内容调整大小,这是有道理的,因为它们跨越了页面的宽度。文章本身的大小根据其内容而定。但是,"title" 和 "nav" 被拆分,这样标题的大小根据内容和 nav 占据了其余的空间。
如果我改用 grid-template-rows: auto auto auto auto;,标题和导航将共享相等的间距,这不是我想要的。
auto auto 1fr auto 是如何解释的,它给了我这样的大小,使得 title 占据了所需的最小空间,而 nav 占据了其余的空间?在这种情况下,“fr”和“auto”如何交互?
main {
display: grid;
grid-template-columns: 10rem auto;
grid-template-rows: auto auto 1fr auto;
grid-template-areas: "header header" "title article" "nav article" "footer footer";
}
header {
grid-area: header;
background: lightblue;
}
div {
grid-area: title;
background: orange;
}
nav {
grid-area: nav;
background: pink;
}
article {
grid-area: article;
background: yellow;
}
footer {
grid-area: footer;
background: lightblue;
}
<main>
<header>This is the header</header>
<div>This is the title</div>
<nav>This is the nav</nav>
<article>
This is the article. This is the article. This is the article. This is the article.
This is the article. This is the article. This is the article. This is the article.
This is the article. This is the article. This is the article. This is the article.
This is the article. This is the article. This is the article. This is the article.
This is the article. This is the article. This is the article. This is the article.
This is the article. This is the article. This is the article. This is the article.
</article>
<footer>This is the footer</footer>
</main>
【问题讨论】:
-
您的网格中有 4 行 - 所有(第 3 行除外)都是
auto大小的。所以第一、第二和第四行auto-sized 和第三行(包含nav)占据了所有剩余空间。尝试设置grid-template-rows: auto 1fr 2fr auto;看看会发生什么。
标签: css grid-layout css-grid