-6

How can I target a element with a class only if it is the first element under the body tag, and then not style any other elements with the same class on the same page?

lky
  • 1,081
  • 3
  • 15
  • 31

1 Answers1

0

You can target an element as direct child of the body only if it's both the first-child and with a specific class name

body > .yourclass:first-child {
   ...
}

:first-child matches a specific element, regardless of its class name but in this case the class must be also chained. All other elements with that class name won't be styled.


Codepen demo

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177