When I used p:nth-child(1){color: white;}
in my stylesheet it didn’t work.
I was expecting the first p tag to be white but it was black.
the tags in the html document was in this order: p, div, form, div, p. I also used a class attribute in the p tags
I was wondering could the div tag have caused it.
2
Answers
I realized adding a
<br />
tag before the<p>
tag in the HTML was the problem but why?It’s possible that the
div
tag is causing the issue. The:nth-child()
selector selects the nth child of its parent element, regardless of its type. In your case, if thediv
tags are the first child elements of their parent, then they will be selected instead of thep
tags.To fix this issue, you can try using the
:first-of-type
selector instead of:nth-child(1)
. This selector selects the first child element of its type** within its parent element. Here’s an example:This will select the first
p
element that is a child of adiv
element and set its color to white.