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?
Asked
Active
Viewed 225 times
-6
-
4Sure, you can! Any other questions? Have you tried anything? – pavel Mar 19 '18 at 15:51
-
1At least share your thoughts. Tell step by step, out loud what you want to accomplish. You would be surprised how declarative `js`, or `css` can be can be. – Learn on hard way Mar 19 '18 at 15:55
1 Answers
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.

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