css - How can I select an nth child without knowing the parent element? -
i can't figure out way of selecting nth element, last element, or first element in cases don't know parent element. nth-child exists, children, example:
<div> <p>one</p> <p>two</p> </div> p:last-child selects "two" paragraph, , p:first-child selects "one" paragraph. when have dynamic code , have no idea parent name is, or parent (may div, span, anchor, ul, etc.)?
for example:
<youdontknowwhat!> <p class="select-me">one</p> <p class="select-me">two</p> </youdontknowwhat!> how select second paragraph here? (i'm unable select youdontknowwhat! since don't know element (it's hypothetical name).
why there first-child, last-child, , nth-child selectors , no :first, :last, :nth (like .select-me:first)?
how
:firstdifferent:first-child? every html element child of other element in dom except<html>root element. – boltclockit'd since don't know parent element. – fomicz
you don't need know parent element :first-child or :nth-child() work. they work, if don't specify parent element.
the following selector guaranteed match appropriate .select-me element regardless of parent element is:
.select-me:nth-child(2)
Comments
Post a Comment