How can I add a nth-child(n)
declaration while applying inline styles to an element (which, obviously, contains multiple elements).
For example, suppose I have a div
that contains three p
elements.
The general stylesheet rules would go like this:
div p:nth-child(2) {
color: blue;
}
But how can I still colour the second paragraph blue while applying an inline style to the containing div
?
An inline style attribute only pertains to the element with the attribute. So for example, if you have a style attribute on a div
, the styles will only apply to that div
(inherited properties and conflicting styles notwithstanding). You cannot target a different element using an inline style attribute on another element.
Even if you apply a style attribute onto a p
element, you can't make the inline styles apply conditionally based on a pseudo-class. See my answer to this question for why. However, if the markup is dynamically generated, you can control whether or not the style attribute gets printed in the first place using similar logic, but that is outside the scope of your question.