html - How to select a div with nth-child()? -


i have html structure this:

<section id="main-content" class="photo-grid">     <h2>photos</h2>      <div class="col-1-4">             <a href="#">                 <p>file 503</p>                 <img src="#" />             </a>     </div>      <div class="col-1-4">             <a href="#">                 <p>file 508</p>                 <img src="#" />             </a>     </div>      <div class="col-1-4">             <a href="#">                 <p>file 505</p>                 <img src="#" />             </a>     </div>      <div class="col-1-4">             <a href="#">                 <p>file 525</p>                 <img src="#" />             </a>     </div>      <br clear="all" /> </section> 

i want set different css properties diferent .col-1-4 elements. how can select them using :nth-child()?

i've tried #main-content:nth-child(n) , .photo-grid:nth-child(n), doesn't seem work.

you should use nth-of-type , not nth-child this, difference between saying 'select nth child element of specified element' vs 'select nth type of element child of specified element':

#main-content div:nth-of-type(1)

demo fiddle

where 1 number (index starts 1) of div in question

you should apply nth-of-type specific type of element being targeted, focus above to:

.col-1-4:nth-of-type(n)

more on nth-child mdn

this can more described way: matching element bth child of element after children have been split groups of elements each.

more on nth-of-type mdn

the :nth-of-type css pseudo-class matches element has an+b-1 siblings same element name before in document tree, given positive or 0 value n, , has parent element. see :nth-child more thorough description of syntax of argument. more flexible , useful pseudo selector if want ensure you're selecting same type of tag no matter inside parent element, or other different tags appear before it.


Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -