{"id":8854,"date":"2011-04-20T06:18:54","date_gmt":"2011-04-20T13:18:54","guid":{"rendered":"http:\/\/css-tricks.com\/?p=8854"},"modified":"2021-11-12T14:44:49","modified_gmt":"2021-11-12T22:44:49","slug":"the-difference-between-nth-child-and-nth-of-type","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/the-difference-between-nth-child-and-nth-of-type\/","title":{"rendered":"The Difference Between :nth-child and :nth-of-type"},"content":{"rendered":"\n

Let’s assume this HTML:<\/p>\n\n\n\n

<section>\n   <p>Little<\/p>\n   <p>Piggy<\/p>    <!-- We want this one -->\n<\/section><\/code><\/pre>\n\n\n\n

These will do the exact same thing:<\/p>\n\n\n\n

p:nth-child(2) { color: red; }<\/code><\/pre>\n\n\n\n
p:nth-of-type(2) { color: red; }<\/code><\/pre>\n\n\n\n

There is a difference though of course.<\/p>\n\n\n\n

Our :nth-child<\/code> selector above, in “Plain English,” means select an element if<\/em>:<\/p>\n\n\n\n

  1. It is a paragraph element<\/li>
  2. It is the second child of a parent<\/li><\/ol>\n\n\n\n

    Our :nth-of-type<\/code> selector, in “Plain English,” means:<\/p>\n\n\n\n

    1. Select the second paragraph child of a parent<\/li><\/ol>\n\n\n\n

      :nth-of-type<\/code> is… what’s a good way to say it… less conditional<\/em>.<\/p>\n\n\n\n

      Let’s say our markup changed to this:<\/p>\n\n\n\n

      <section>\n   <h1>Words<\/h1>\n   <p>Little<\/p>\n   <p>Piggy<\/p>    <!-- We want this one -->\n<\/section><\/code><\/pre>\n\n\n\n

      This breaks:<\/p>\n\n\n\n

      p:nth-child(2) { color: red; } \/* Now incorrect *\/<\/code><\/pre>\n\n\n\n

      This still works:<\/p>\n\n\n\n

      p:nth-of-type(2) { color: red; } \/* Still works *\/<\/code><\/pre>\n\n\n\n

      By “breaks”, I mean that the :nth-child<\/code> selector above is now selecting the word “Little” instead of “Piggy” because that element fulfills both 1) it’s the second child and 2) it’s a paragraph element. By “still works,” I mean that “Piggy” is still being selected because it’s the second paragraph under that parent element.<\/p>\n\n\n\n

      If we were to add an <h2><\/code>\u00a0after that\u00a0<h1><\/code>, the :nth-child selector\u00a0wouldn\u2019t select anything at all<\/em>, because now the second child is no longer a paragraph so that selector finds nothing. The\u00a0:nth-of-type<\/code>\u00a0again still works.<\/p>\n\n\n\n

      I feel like :nth-of-type<\/code> is less fragile and more useful in general, despite :nth-child<\/code> being more common (in my eyes). How often do you think \u201cI want to select the second child of a parent if it just happens to be a paragraph.\u201d Possibly sometimes, but more likely you want to \u201cselect the second paragraph\u201d or \u201cselect every third table row,\u201d which are cases where :nth-of-type<\/code> is (again, in my eyes) a stronger choice.<\/p>\n\n\n\n

      I find most of my \u201ccrap, why isn\u2019t this :nth-child selector working?!\u201d<\/em> moments are because it turns out I\u2019ve tag-qualified the selector and that number child isn\u2019t really that tag. So when using :nth-child<\/code>, I find it\u2019s generally best to specify the parent and leave :nth-child<\/code> un-tag-qualified.<\/p>\n\n\n\n

      dl :nth-child(2) {  } \/* is better than *\/\ndd:nth-child(2) {  } \/* this *\/<\/code><\/pre>\n\n\n\n

      But, of course, it all depends on the exact situation.<\/p>\n\n\n\n

      Browser support for :nth-of-type<\/code> is fairly decent\u2026 Firefox 3.5+, Opera 9.5+, Chrome 2+, Safari 3.1+, IE 9+.<\/p>\n\n\n\n

      I\u2019d say if you need deeper support, jQuery would have your back (use the selector there, apply a class, and style with that class), but in fact jQuery dropped support for :nth-of-type. Seems weird to me. I heard it was because of low usage. If you want to go that route\u00a0here is a plugin to get them back<\/a>.<\/s>\u00a0jQuery 1.9 does now support :nth-of-type<\/code> again (back to IE 6), so that is an option.<\/p>\n\n\n\n

      Related: don\u2019t forget about the awesome cousins :first-of-type<\/code>, :last-of-type<\/code>, :nth-last-of-type<\/code> and :only-of-type<\/code>. Learn more here.<\/a><\/p>\n\n\n\n

      If you want to play around with a visual example, check out this tool!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

      These are different pseudo class selectors that do slightly different things. In my opinion, :nth-child is more common but :nth-of-type is more useful.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[4],"tags":[],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":14771,"url":"https:\/\/css-tricks.com\/a-call-for-nth-everything\/","url_meta":{"origin":8854,"position":0},"title":"A Call for ::nth-everything","date":"October 31, 2011","format":false,"excerpt":"We already have ::first-letter and ::first-line which are useful. Why not expand those out into a full set of selectors that follow the same pattern of the ::nth-child family? I attempt to make the case here.","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2011\/10\/lettering.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":18936,"url":"https:\/\/css-tricks.com\/updated-nth-tester\/","url_meta":{"origin":8854,"position":1},"title":"Updated :nth Tester","date":"October 18, 2012","format":false,"excerpt":"Just a quick post to note that I've updated the :nth Tester page to hopefully be a bit more useful than it was before. The purpose of the page is to learn in an interactive way how the selectors :nth-child, :nth-of-type, :nth-last-child, and :nth-last-of-type work. In this update, I: Made\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":254901,"url":"https:\/\/css-tricks.com\/extremely-handy-nth-child-recipes-sass-mixins\/","url_meta":{"origin":8854,"position":2},"title":"Some Extremely Handy `:nth-child` Recipes as Sass Mixins","date":"May 17, 2017","format":false,"excerpt":"There is no such thing as one-size-fits-all styling. An image gallery with three images might need to be styled differently than an image gallery with twelve. There are some cool tricks that you can use to add some number-based logic to your CSS! Using :nth-child and :nth-last-child, you can get\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9657,"url":"https:\/\/css-tricks.com\/useful-nth-child-recipies\/","url_meta":{"origin":8854,"position":3},"title":"Useful :nth-child Recipes","date":"June 16, 2011","format":false,"excerpt":"I get a little giddy when I come across perfect uses for :nth-child or :nth-of-type (read about the difference). The better you understand them, the more css nerdgasms you get to have!","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":305459,"url":"https:\/\/css-tricks.com\/css-nth-of-class-selector\/","url_meta":{"origin":8854,"position":4},"title":"CSS :nth-of-class selector","date":"March 23, 2020","format":false,"excerpt":"That's not a thing. But it kinda is! Bram covers how frustrating .bar:nth-child(2) is. It's not \"select the second element of class .bar.\" It's \"select the second element if it also has the class .bar.\" The good news? There is a real selector that does the former: :nth-child(2 of .bar)\u2026","rel":"","context":"In "Link"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/03\/nth-selector.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":342736,"url":"https:\/\/css-tricks.com\/nth-child-between-two-fixed-indexes\/","url_meta":{"origin":8854,"position":5},"title":":nth-child Between Two Fixed Indexes","date":"June 29, 2021","format":false,"excerpt":"I needed to select some elements between two fixed indexes the other day\u2009\u2014\u2009like literally the second through fifth elements. Ironically, I have a whole post on \"Useful :nth-child Recipes\" but this wasn't one of them. The answer, it turns out, isn't that complicated. But it did twist my brain a\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/03\/nth-selector.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]}],"featured_media_src_url":null,"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/8854"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=8854"}],"version-history":[{"count":9,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/8854\/revisions"}],"predecessor-version":[{"id":356879,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/8854\/revisions\/356879"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=8854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=8854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=8854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}