Assuming the reason you want to apply the nth-child(n)
declaration as an inline style is because you can't edit the CSS file or don't want to; but rather you need to apply your styling directly on the page, you could try just adding a <style>
tag next to the div in question:
<style>
div.myContainer p:nth-child(2) {
color: blue;
}
</style>
<div class="myContainer">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
I should note: this is not the ideal way to structure your code. Styles and HTML/content should be separated to create properly formatted and semantic markup.
Also, if you have to apply this styling in more than one place, it can become messy and/or inconsistent. However, I understand that sometimes you have to make exceptions depending on the project.
nth-child
selectors on the div's childp
elements to override the inline styles? In which case, the answer is still no (see Huangism's first comment), but @Evoke 's comment is totally unrelated to the question at hand; OP didn't make a single mention of JS (where'd you get that from?) and already showed a correct application of pseudo classes. – Cook