That is because the selector does now look at the entire document, it looks at the parent. Any time an <h1>
is the first child of any element, it will match to that selector. If you only want it to apply to one single <h1>
in a document, considering giving it a separate class or ID, or selecting it more specifically based on where you expect it to appear.
For example, on my site I separate each chunk of text into a <div class="box">
which are all present in the <body>
of the document. So if I wanted to match only the first <h1>
in the document, I could do something like this:
body > .box:first-child > h1:first-of-type { }
This would select the first box only, and then match the first <h1>
in that box, simulating the "first <h1>
in the document" effect (assuming the first box has an <h1>
, which on my website is always true if one exists). I assume you wanted to use the :first-of-type
selector here, because the first <h1>
in a document doesn't necessarily have to be the first child of a parent.