How can I specify the first p
element of each li
tag belongs to the same class without doing it manually (in the case there are too many li
tags in the ul
element), as we know that :first-child
pseudo-class only applies style
attribute for element, not class
attribute?
<ul>
<li>
<p>...</p>
<p>...</p>
</li>
<li>
<p>...</p>
<p>...</p>
</li>
</ul>
2
Answers
You can use
li > p:first-child
as selector in the CSS:If you really want to add a class to those elements then you can use JavaScript for it and the
classList.add()
function:Not sure if I get it your question or not. But my understanding is you want to select paragraphs that shares some specific class that under li element. If that is the case you can inspect the code below.