<分区>
我正在使用 css 网格,但遇到了一个奇怪的问题。
正如您在下面的代码中看到的那样。我有一个父网格,homepage__section__medical-types__content
声明了
grid-template-areas:
"blue"
"content1"
"green"
"content2";
和四行。 content1 和 content2 分别分配给:
homepage__heading__info:first-child
和
homepage__heading__info:nth-child(2)
但为什么它们的显示顺序与声明的顺序相反?我做错了什么?
body {
margin: 0;
display: flex;
justify-content: center;
}
.container {
background-color: lavenderblush;
height: auto;
width: 320px;
}
.homepage__section__medical-types {
display: grid;
grid-template-areas: ". medic .";
grid-template-columns: 48px 1fr 16px;
}
.homepage__section__medical-types__content {
grid-area: medic;
display: grid;
grid-template-areas:
"blue"
"content1"
"green"
"content2";
grid-template-rows: 250px 1fr 250px 1fr;
}
.homepage__heading-box {
height: 100%;
border-radius: 25px;
}
.homepage__heading-box--blue {
grid-area: blue;
background-image: linear-gradient(to bottom, #669DD6, #ADDAF1);
}
.homepage__heading-box--green {
grid-area: green;
background-image: linear-gradient(to bottom, #51CEA2, #97DCC6);
}
.homepage__heading__info {
background-color: aliceblue;
}
.homepage__heading__info:first-child {
grid-area: content1;
}
.homepage__heading__info:nth-child(2) {
grid-area: content2;
color: red
}
<div class="container">
<div class="homepage__section__medical-types">
<div class="homepage__section__medical-types__content">
<div class="homepage__heading-box homepage__heading-box--blue"></div>
<div class="homepage__heading__info">
<p>1</p>
</div>
<div class="homepage__heading-box homepage__heading-box--green"></div>
<div class="homepage__heading__info">
<p>2</p>
</div>
</div>
</div>
</div>
编辑:我注意到只是注释掉 grid-area: content1;
和 grid-area: content2;
但保持父 grid-template-area 相同, 相应的元素以正确的顺序显示。我比以前更困惑了。