html - nth-of-type(2) is targeting the first of type? -
i have relatively modular page due cms, content of these paragraph blocks don't fill whole block, want increase font size of blocks. however, when try target 2 , 3, doesn't seem recognize @ all. in fact, targets only first one, , in inspector, says because it's applying rule 2.
example html
<div class="container"> <div class="item video"> <!-- html video stuffs--> </div> <div class="item copy"> <h1>example title</h1> <p>example words</p> </div> <div class="item photo"> <!-- html photo stuffs--> </div> <div class="item video"> <!-- html video stuffs--> </div> <div class="item copy"> <h1>example title</h1> <p>example words</p> </div> <div class="item photo"> <!-- html photo stuffs--> </div> <div class="item video"> <!-- html video stuffs--> </div> <div class="item copy"> <h1>example title</h1> <p>example words</p> </div> <div class="item photo"> <!-- html photo stuffs--> </div> <div class="item video"> <!-- html video stuffs--> </div> <div class="item copy"> <h1>example title</h1> <p>example words</p> </div> <div class="item photo"> <!-- html photo stuffs--> </div> </div>
now less file looks this:
.container { .item { &.copy { p { font-size: 16px; } &:nth-of-type(2), &:nth-of-type(3) { p { font-size: 17px; } } } } }
theoretically, paragraph text in first , fourth .copy blocks should have font size of 16px, while second , third blocks should 17px. however, i'm seeing of them 16px , first 1 17px, highlighted saying it's because rule ".container .item.copy:nth-of-type(2) p" overwriting it.
why nth-of-type(2) rule targeting first block of it's type , not second? , why isn't nth-of-type(3) rule not targeting second block of it's type if that's how it's calculating them? i'm confused what's happening here... appreciated.
:nth-of-type seeks among elements of type of given class selected (".item.copy"). element div css rule applied div in html structure level.
the solution using :nth-child seeks elements given class (".item.copy") in html structure level. can used select every repeated pattern. that's because can write example this:
.item:nth-child(2n - 1) {} // targets 1st, 3rd, 5th, 7th,...
just careful level in html structure. there's common mistake when there links (always 1 link in li) nested in li elements, when want target every second link, have write this:
ul li:nth-child(2n) { { //style link } }
for using nth-child selector, there useful feature: https://css-tricks.com/examples/nth-child-tester/
Comments
Post a Comment