有没有办法使网格区域的表现像Flexbox?
假设我在相同级别上有一个任意数量的元素:
<container>
<div class="h">
<div class="b">
<div class="h">
<div class="h">
<div class="b">
<div class="hl">
<div class="hr">
<div class="hl">
<div class="hr">
<div class="h">
<div class="f">
</container>
我想要一个布局,就像
grid-template-area:
"h h"
"hl hr"
"b b"
"f f"
我可以将所有内容都放在正确的位置,以> grid-area
,但随后一切都沿z堆叠了使用权。因此,我考虑的两种方式是:
有没有办法定义一个命名的网格区域以表现得像弹性箱一样?类似的伪代码:
定义网格区域h:显示:flex;挠性方向:列;
或2)使每个命名区域都有自动行?因此,如果我
.h {
grid-area: h;
}
和我有3级与H类有3个divs
,这意味着
grid-template-area:
"h h"
"h h"
"h h"
"hl hr"
"b b"
"f f"
或者我还没有想过要完成这种布局的其他想法?
代码段:
.container {
display: grid;
grid-template-areas: "h h" "h h" "h h" "hl hr" "b b" "f f"
}
.h {
grid-area: h;
}
.hl {
grid-area: hl;
}
.hr {
grid-area: hr;
}
.b {
grid-area: b;
}
.f {
grid-area: f;
}
<div class="container">
<div class="h">h1</div>
<div class="b">b1</div>
<div class="h">h2</div>
<div class="h">h3</div>
<div class="b">b2</div>
<div class="hl">hl1</div>
<div class="hr">hr1</div>
<div class="hl">hl2</div>
<div class="hr">hr2</div>
<div class="h">h3</div>
<div class="f">f1</div>
</div>
Say I have an arbitrary number of elements at the same level:
<container>
<div class="h">
<div class="b">
<div class="h">
<div class="h">
<div class="b">
<div class="hl">
<div class="hr">
<div class="hl">
<div class="hr">
<div class="h">
<div class="f">
</container>
And I want a layout that's something like
grid-template-area:
"h h"
"hl hr"
"b b"
"f f"
I can put everything in the right spot with grid-area
, but then everything just gets stacked along the z access. So, the two ways I'm thinking about this are either:
is there a way to define a named grid area to behave like a flexbox? Something like, in pseudocode:
define grid area h: display: flex; flex-direction: column;
or 2) make each named area have auto rows? So, if I
.h {
grid-area: h;
}
and I have 3 divs
with class h, it implies
grid-template-area:
"h h"
"h h"
"h h"
"hl hr"
"b b"
"f f"
Or maybe there's some other idea I haven't thought of to accomplish this layout?
Code Snippet:
.container {
display: grid;
grid-template-areas: "h h" "h h" "h h" "hl hr" "b b" "f f"
}
.h {
grid-area: h;
}
.hl {
grid-area: hl;
}
.hr {
grid-area: hr;
}
.b {
grid-area: b;
}
.f {
grid-area: f;
}
<div class="container">
<div class="h">h1</div>
<div class="b">b1</div>
<div class="h">h2</div>
<div class="h">h3</div>
<div class="b">b2</div>
<div class="hl">hl1</div>
<div class="hr">hr1</div>
<div class="hl">hl2</div>
<div class="hr">hr2</div>
<div class="h">h3</div>
<div class="f">f1</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎不是,但是,就像一个选项一样,您可以使用
顺序更改单元格的顺序:n
属性并定义单元大小以适合您的布局,请参阅:Seems like not, but, like an option, you can change order of cells by using
order: n
property and define cell sizes to fit your layout, see: