html - 为什么使用具有相同类但不同选择器的网格区域以相反顺序显示的网格模板区域?

标签 html css css-grid

<分区>

我正在使用 css 网格,但遇到了一个奇怪的问题。

正如您在下面的代码中看到的那样。我有一个父网格,homepage__section__medical-types__content 声明了

grid-template-areas:
    "blue"
    "content1"
    "green"
    "content2";

和四行。 content1content2 分别分配给:

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 相同, 相应的元素以正确的顺序显示。我比以前更困惑了。

最佳答案

您误解了 nth-child() 的目的伪类。

明确地说,这是您的 HTML:

<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>

这是您的 CSS:

.homepage__heading__info:first-child {
   grid-area: content1;
}

.homepage__heading__info:nth-child(2) {
   grid-area: content2;
   color: red
}

因此,:first-child 适用于所有 sibling 中的第一个 child 。 .homepage__heading__info 永远不是所有 sibling 中的第一个 child 。所以在应用 :first-child 时,它会被忽略。如果您在开发工具中搜索此选择器,它甚至不会出现,因为它的目标不存在。

对于 .homepage__heading__info:nth-child(2),这实际上适用于所有 sibling 中的第二个 child ,恰好是第一个 .homepage__heading__info(元素带有文本“1”)。根据 grid-template-areas,选择器将网格区域“content2”应用于此元素,并将其发送到底部。

grid-template-areas:
   "blue"
   "content1"
   "green"
   "content2"

带有文本“2”的元素被自动放置在未占用的行中,恰好是第 2 行(同样,content1 不存在)。结果,2 在 1 之前。


EDIT: I have noticed that just commenting out grid-area: content1 and grid-area: content2 but keeping the same the parent grid-template-area, the corresponding elements are displayed in the correct order. I am more confused then before.

如果没有网格区域,算法会将指定元素放置在每个 grid-template-areas 中(在这种情况下,只会是“蓝色”和“绿色”),其余元素是自动的-按源顺序放入现有行。

关于html - 为什么使用具有相同类但不同选择器的网格区域以相反顺序显示的网格模板区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57713267/

相关文章:

html - 尽管验证服务给我 100%,但 CSS Grid 仍无法正常运行?

javascript - JQuery 将项目添加到我的 JSON

javascript - 如何在表格中查找下一个直接下载单元格

jquery - 是否可以使用没有 ul li 标签的 jquery ui 标签

html - 将图标字体字形与文本对齐

css - 全 Angular 元素破坏了使用显示 : grid 创建的布局

html - @media 取决于缩放系数?

JavaScript document.location.href 似乎期望 HTML

html - IE11 忽略内联 CSS

css - 网格垂直滚动