样式只是文档中的第一个h1

Con*_*Del 2 html css css-selectors

我正在寻找一种方式来塑造第一个<h1>后代<body>,或者第一个<h2>后代<div>.我尝试使用,:first-child但这似乎是任何第一个孩子......

有没有办法在纯CSS中指定"first <h1>within <body>"或"first <h2>in every <div>"?

j08*_*691 8

你可能想要:first-of-type:

div > h2:first-of-type {
  color: red;
}

body > h1:first-of-type {
  color: red;
}
Run Code Online (Sandbox Code Playgroud)