Style every <p> element that is the second child of its parent with CSS

CSSWeb DevelopmentFront End Technology

<p style="">To style every &lt;p&gt; element that is the second child of its parent with CSS, use the CSS :nth-child(n) selector.</p><h2>Example</h2><p>You can try to run the following code to implement the :nth-child(n) selector</p><p style=""><a class="demo" href="http://tpcg.io/7HgTcr" rel="nofollow noopener noreferrer" target="_blank">Live Demo</a></p><pre class="prettyprint notranslate">&lt;!DOCTYPE html&gt; &lt;html&gt; &nbsp; &nbsp;&lt;head&gt; &nbsp; &nbsp; &nbsp; &lt;style&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p:nth-child(4) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background: orange; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: white; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &lt;/style&gt; &nbsp; &nbsp;&lt;/head&gt; &nbsp; &nbsp;&lt;body&gt; &nbsp; &nbsp; &nbsp; &lt;p&gt;This is demo text 1.&lt;/p&gt; &nbsp; &nbsp; &nbsp; &lt;p&gt;This is demo text 2.&lt;/p&gt; &nbsp; &nbsp; &nbsp; &lt;p&gt;This is demo text 3.&lt;/p&gt; &nbsp; &nbsp; &nbsp; &lt;p&gt;This is demo text 4.&lt;/p&gt; &nbsp; &nbsp; &nbsp; &lt;p&gt;This is demo text 5.&lt;/p&gt; &nbsp; &nbsp; &nbsp; &lt;p&gt;This is demo text 6.&lt;/p&gt; &nbsp; &nbsp;&lt;/body&gt; &lt;/html&gt;</pre>
raja
Updated on 30-Jun-2020 11:22:25

Advertisements