我正在做一个作业,我想像这样使两组css-grids相互混合:
我正在使用以下代码
.group1 .item1 {
grid-column: 1 / 4;
}
.group1 .item2 {
grid-column: 1;
}
.group1 .item3 {
grid-column: 2 / 4;
}
.group2 .item4 {
grid-column: 2 / 3;
}
.group2 .item5 {
grid-column: 3 / 4;
}
.group2 .item6 {
grid-column: 2 / 4;
}
.container {
display: grid;
grid-gap: 5px;
max-width: 1200px;
margin: 0 auto 100px auto;
border: 8px dashed #999;
}
<section class="part5 container">
<div class="container group1">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
<div class="container group2">
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
</section>
我希望输出与附件中的[image]类似,没有更改HTML ,但是我无法获得该输出,请帮助我,对此我将非常感谢善良的行为。
答案 0 :(得分:1)
您可以使用display:contents来避免子容器进入,并使用display grid和grid-area(grid-row / grid-column)来分发元素。
但这还不是到处都有效!
想法演示
.part5 {
display: grid;
grid-template-colums: repeat(6, 1fr);
min-height: 100vh
}
.container.group1,
.container.group2 {
display: contents;
}
.item1 {
grid-column: 1/ span 6;
grid-row: 1;
border: solid;
color: tomato;
}
.item2 {
grid-row: 2 /span 3;
grid-column: 1 /span 2;
border: solid;
color: turquoise;
}
.item3 {
grid-row: 2;
grid-column: 3/span 4;
border: solid;
color: green;
}
.item4 {
grid-row: 3;
grid-column: 3 /span 2;
border: solid;
}
.item5 {
grid-row: 3;
grid-column: 5 / span 2;
border: solid;
color: brown;
}
.item6 {
grid-row: 4;
grid-column: 3 / span 4;
border: solid;
color: purple;
}
/* demo*/
* {
margin: 0;
}
[class^=item] {
display: flex;
align-items: center;
justify-content: center;
font-size: calc(2vh + 2vw)
}
<section class="part5 container">
<div class="container group1">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
<div class="container group2">
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
</section>
https://css-tricks.com/get-ready-for-display-contents/
-一种神奇的新显示值,从根本上使容器消失,使子元素的子元素成为DOM中更高一级的子元素。
您的代码中可能是短代码更新:
/*update */
.container {
display: contents
}
.part5 {
/* end update */
display: grid;
grid-gap: 5px;
max-width: 1200px;
margin: 0 auto 100px auto;
border: 8px dashed #999;
}
.group1 .item1 {
grid-column: 1 / 4;
}
.group1 .item2 {
grid-column: 1;
grid-row: 2/5;
}
.group1 .item3 {
grid-column: 2 / 4;
}
.group2 .item4 {
grid-column: 2 / 3;
}
.group2 .item5 {
grid-column: 3 / 4;
}
.group2 .item6 {
grid-column: 2 / 4;
}
.container {
display: contents
}
.part5 {
display: grid;
grid-gap: 5px;
max-width: 1200px;
margin: 0 auto 100px auto;
border: 8px dashed #999;
}
/*demo*/
div {
box-shadow: 0 0 0 2px lightgray;
<section class="part5 container">
<div class="container group1">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
<div class="container group2">
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
</section>
粗略的方法是将两个组设置在重叠的同一网格上:
.container {
display: grid;
width: 100%;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(4, 1fr);
}
.group1 {
grid-row: 1 / span 4;
grid-column: 1 / span 6;
}
.group2 {
grid-template-rows: repeat(2, 1fr);
grid-column: 3 /span 4;
grid-row: 3 /span 3;
}
.item1 {
grid-column: 1 / span 6;
color: tomato;
}
.item2 {
grid-column: 1 / span 2;
grid-row: 2 / span 4;
color: turquoise;
}
.item3 {
grid-column: 3 / span 4;
color: green;
}
.item4 {
grid-column: 1 /span 3;
grid-row: 1;
}
.item5 {
grid-column: 4/span 3;
color: brown;
}
.item6 {
grid-column: 1/ span 6;
color: purple;
}
/* demo*/
[class^=item] {
border: solid;
display: flex;
align-items: center;
justify-content: center;
font-size: calc(2vh + 2vw);
background: lightgray;
min-height:20vh
}
<section class="part5 container">
<div class="container group1">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
<div class="container group2">
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
</section
答案 1 :(得分:0)
display: subgrid
解决此问题的一种干净有效的方法是使用display: subgrid
,这是专为此类布局设计的CSS Grid功能。子网格允许嵌套的网格容器识别主网格容器的网格线。
很遗憾,此功能尚不可用。这里有更多详细信息:
grid-template-areas
解决此问题的另一种干净有效的方法是将主容器(.part5.container
)设置为网格容器,然后使用grid-template-areas
将两个子容器排列成所需的形状。
很遗憾,此功能也尚不可用。这里有更多详细信息:
因此,这是一个使用CSS网格的解决方案,并且(为了补偿上面列出的缺失功能)有一点绝对定位。无需更改HTML。
.part5.container {
display: grid;
border: 8px dashed #999;
height: 100vh;
grid-template-rows: auto auto;
grid-template-columns: 35% 1fr;
grid-template-areas: " group1 group1 "
" . group2 ";
}
.container.group1 {
grid-area: group1;
display: grid;
grid-template-rows: 50px 1fr;
grid-template-columns: 35% 1fr;
grid-gap: 5px;
position: relative;
}
.item1 {
grid-column: 1 / -1;
}
.item2 {
position: absolute;
top: 55px; /* top row height plus gap */
width: 35%; /* first column width */
height: calc(100vh - 71px); /* minus height of top row (50px) plus borders (16px)) */
}
.item3 {
grid-column: 2;
}
.container.group2 {
grid-area: group2;
display: grid;
grid-template-rows: 1fr 50px;
grid-template-columns: 1fr 1fr;
grid-gap: 5px;
margin: 5px 0 0 5px;
}
.item6 {
grid-column: 1 / -1;
}
.item {
background-color: lightgreen;
display: flex;
align-items: center;
justify-content: center;
}
body {
margin: 0;
}
* {
box-sizing: border-box;
}
<section class="part5 container">
<div class="container group1">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
</div>
<div class="container group2">
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
</section>