英文:
css grid creates empty rows and columns
问题
我有一个包含2个区域的网格。如果我使用grid-template-areas: "list details"
,它可以正常工作。为什么当我只指定一个区域时,使用grid-template-areas: "list"
它会创建一个3x2的网格呢?而且为什么它将第二个区域放在底部右侧轨道上呢?
链接:https://jsfiddle.net/o5txebkc/
#main {
display: grid;
grid-template-areas:
"list";
}
#main .list {
background: yellow;
grid-area: list;
}
#main .details {
background: green;
grid-area: details;
}
<main id="main">
<article class="list">
<ul>
<li>one</li>
<li>two</li>
</ul>
</article>
<article class="details">
<h3>Details</h3>
<p>
一些详情
</p>
</article>
</main>
英文:
I have a grid with 2 areas. It works fine with grid-template-areas: "list details"
. Why does it create a 3x2 grid if I specify only one area: grid-template-areas: "list"
? And why does it stick the second area in the bottom right track?
https://jsfiddle.net/o5txebkc/
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
#main {
display: grid;
grid-template-areas:
"list";
}
#main .list {
background: yellow;
grid-area: list;
}
#main .details {
background: green;
grid-area: details;
}
<!-- language: lang-html -->
<main id="main">
<article class="list">
<ul>
<li>one</li>
<li>two</li>
</ul>
</article>
<article class="details">
<h3>Details</h3>
<p>
some details
</p>
</article>
</main>
<!-- end snippet -->
答案1
得分: 1
使用开发者工具来突出显示网格并显示线条,我看到"details"位于显式网格之外。(注意负线号。)如果从*.details { }*类规则中删除grid-area: details;
,则此行为将更改。
因为您为"details"定义了一个grid-area,但没有在网格上明确指定位置,它将放置在最接近的行和列,而不会触及命名线。
- 显式网格:使用grid-template[-*]创建。
- 隐式网格:当内容放置在网格之外时,通过绘制额外的网格线扩展已定义的显式网格,例如进入行中。
我们也可以从网格的块和内联结束倒数计数... 这些线可以用-1表示,您可以从那里开始倒数... 所以倒数第二根线是-2。 值得注意的是,最后一根线是显式网格的最后一根线... 不考虑在隐式网格之外添加的任何行或列。
如果名称以<custom-ident>的形式给出,只会计算具有该名称的线。如果没有足够的具有该名称的线,就会假定所有隐式网格线都具有该名称,以便找到此位置。
英文:
Using Developer Tools to highlight the grid and show the lines, I see that "details" is positioned outside of the explicit grid. (Notice the negative line numbers.) This behavior changes if you remove grid-area: details;
from the .details { } class rule.
Because you defined a grid-area for "details" but did not give it an explicit position on the grid, it is placed into the implicit grid at the closest row and column where it is not touching the named lines.
<br>The implicit and explicit grid
- Explicit grid: Created using grid-template[-*].
- Implicit grid: Extends the defined explicit grid when content is placed outside of that grid, such as into rows by drawing additional grid lines.
<br>Counting backwards
> We can also count backwards from the block and inline end of the grid... These lines can be addressed as -1, and you can count back from
> there – so the second last line is -2. It is worth noting that the
> final line is the final line of the explicit grid ... and does not take into
> account any rows or columns added in the implicit grid outside of
> that.
<br>'grid-area' values
> If a name is given as a <custom-ident>, only lines with that name are
> counted. If not enough lines with that name exist, all implicit grid
> lines are assumed to have that name for the purpose of finding this
> position.
答案2
得分: -1
尝试这个:
<!-- 开始代码段: JavaScript 隐藏: false 控制台: true Babel: false -->
<!-- 语言: CSS -->
#main {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
#main .list {
background: yellow;
}
#main .details {
background: green;
}
<!-- 语言: HTML -->
<main id="main">
<article class="list">
<ul>
<li>one</li>
<li>two</li>
</ul>
</article>
<article class="details">
<h3>Details</h3>
<p>
some details
</p>
</article>
</main>
<!-- 结束代码段 -->
请注意,我已经将原始内容中的HTML和CSS代码进行了翻译和排版,以便更清晰地呈现出来。
英文:
Try this
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
#main {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
#main .list {
background: yellow;
}
#main .details {
background: green;
}
<!-- language: lang-html -->
<main id="main">
<article class="list">
<ul>
<li>one</li>
<li>two</li>
</ul>
</article>
<article class="details">
<h3>Details</h3>
<p>
some details
</p>
</article>
</main>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论