随着CSS网格布局模块很快在Firefox和Chrome中发布,我认为我试图了解如何使用它。
我尝试创建一个简单的网格,其中一个项a
跨越所有行的左侧,其他项目b
,c
,{ {1}},d
等)跨越各行的右侧。跨越行右侧的项目数量是可变的,因此可能存在e
,b
,c
,d
等的任意组合,因此我和#39; m使用e
属性。因此,我无法为grid-auto-rows
定义固定数量的行,但我希望a
跨越所有可用行。
a

#container {
display: grid;
grid-auto-flow: column;
grid-auto-rows: auto;
grid-template-columns: [left] 4rem [right] 1fr;
margin: 0rem auto;
max-width: 32rem;
}
#a {
background: lightgreen;
grid-column: left;
grid-row: 1 / auto;
justify-self: center;
}
#b {
grid-area: auto / right;
background: yellow;
}
#c {
grid-area: auto / right;
background: pink;
}
#d {
grid-area: auto / right;
background: lightskyblue;
}
#e {
background: plum;
grid-area: auto / right;
}

如果<div id="container">
<div id="a">a</div>
<div id="b">b</div>
<div id="c">c</div>
<div id="d">d</div>
<div id="e">e</div>
</div>
跨越所有行而不知道最终会有多少行,我应该怎么做?
答案 0 :(得分:38)
我遇到了同样的情况并找到了一个干净的解决方案。
不要使用巨大的行跨度值,请尝试:
grid-column: 1/-1;
由于负数从右侧开始计算,此代码将网格列指定为最后一列的末尾。
答案 1 :(得分:0)
你可能会使用行的hudge值来跨越(至少你认为行数最多可能是这样):
#container {
display: grid;
grid-auto-flow: column;
grid-auto-rows: auto;
grid-template-columns: [left] 4rem [right] 1fr;
margin: 0rem auto;
max-width: 32rem;
}
#a {
background: lightgreen;
grid-column: left;
grid-row-start: 1;
grid-row-end: span 1000;/* hudge value ... will at least span so many rows */
justify-self: center;/* ? what did you mean here ? */
/* did you mean : */
display:flex;
align-items:center;
}
#b {
grid-area: auto / right;
background: yellow;
}
#c {
grid-area: auto / right;
background: pink;
}
#d {
grid-area: auto / right;
background: lightskyblue;
}
#e {
background: plum;
grid-area: auto / right;
}
<div id="container">
<div id="a">a</div>
<div id="b">b</div>
<div id="c">c</div>
<div id="d">d</div>
<div id="e">e</div>
</div>
或者你的意思是:
#container {
display: grid;
grid-auto-flow: column;
grid-auto-rows: auto;
grid-template-columns: [left] 4rem [right] 1fr;
margin: 0rem auto;
max-width: 32rem;
}
#a {
background: lightgreen;
grid-column: left;
grid-row-start: 1;
grid-row-end: span 1000;/* hudge value ... will at least span so many rows */
align-self: center;
justify-self:center
}
#b {
grid-area: auto / right;
background: yellow;
}
#c {
grid-area: auto / right;
background: pink;
}
#d {
grid-area: auto / right;
background: lightskyblue;
}
#e {
background: plum;
grid-area: auto / right;
}
<div id="container">
<div id="a">a</div>
<div id="b">b</div>
<div id="c">c</div>
<div id="d">d</div>
<div id="e">e</div>
</div>