CodePen<\/a><\/div>\nAs you probably guessed, you could also use other selector\/selector combinations such as :only-child<\/code>, :first-child:last-child<\/code>, :first-child:not(:last-child)<\/code> and the like. But I have found that :nth-child(x):nth-last-child(x)<\/code> gives the most flexibility<\/strong> and serves as a built-in fallback (as it allows us to target the element directly, rather than by exclusion), and there is little cross browser support advantage gained by using the other selector\/selector combinations.<\/p>\nThat’s it. Simple, graceful and totally automatic; the way it ought to be. Support for this is almost universal, except for IE, which eats glue and only offers support in IE9+. At the time of writing, estimated global support for these selectors, as used, was roughly 87% which is not too bad. <\/p>\n","protected":false},"excerpt":{"rendered":"
The following is a guest post by Ray Messina. Ray was interested in sharing this technique as a way to pay forward things he’s learned from this site in the past, which is awesome. You might be aware of the jQuery .has method, which allows you to select an element if it has any of […]<\/p>\n","protected":false},"author":248413,"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":true,"jetpack_social_options":[]},"categories":[4],"tags":[],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":270203,"url":"https:\/\/css-tricks.com\/solved-with-css-dropdown-menus\/","url_meta":{"origin":150992,"position":0},"title":"Solved with CSS! Dropdown Menus","date":"May 1, 2018","format":false,"excerpt":"A common UI pattern that we see on the web are dropdown menus. They\u2019re used to display related information in pieces, without overwhelming the user with buttons, text, and options. Somewhere that we see these a lot is inside of headers or navigation areas on websites. Let\u2019s see if we\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/04\/dropdown-menu-collage.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":333285,"url":"https:\/\/css-tricks.com\/i-saw-two-mega-menus-today\/","url_meta":{"origin":150992,"position":1},"title":"I Saw Two Mega Menus Today…","date":"February 2, 2021","format":false,"excerpt":"One was the footer of an (older) U.S. Government website. The other was the navigation for AWS services from the AWS Console.","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/01\/Screen-Shot-2021-01-20-at-10.50.52-AM.png?fit=1200%2C1097&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":19174,"url":"https:\/\/css-tricks.com\/off-canvas-menu-with-css-target\/","url_meta":{"origin":150992,"position":2},"title":"Off Canvas Menu with CSS :target","date":"November 7, 2012","format":false,"excerpt":"\"Off Canvas\" patterns are different ways to approach layout where content on the web isn't just laid out in a vertical column. For instance, navigation could be positioned hidden off the left edge of the \"canvas\" (visible browser window) and slid in on demand. Anthony Colangelo created jPanelMenu to do\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":276762,"url":"https:\/\/css-tricks.com\/one-invalid-pseudo-selector-equals-an-entire-ignored-selector\/","url_meta":{"origin":150992,"position":3},"title":"One Invalid Pseudo Selector Equals an Entire Ignored Selector","date":"October 5, 2018","format":false,"excerpt":"Perhaps you know this one: if any part of a selector is invalid, it invalidates the whole selector. For example: div, span::butt { background: red; } Even though div is a perfectly valid selector, span:butt is not, thus the entire selector is invalidated \u2014 neither divs nor span::butt elements on\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/09\/pseudo-selector-fail.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":334666,"url":"https:\/\/css-tricks.com\/how-we-improved-the-accessibility-of-our-single-page-app-menu\/","url_meta":{"origin":150992,"position":4},"title":"How We Improved the Accessibility of Our Single Page App Menu","date":"February 25, 2021","format":false,"excerpt":"I recently started working on a Progressive Web App (PWA) for a client with my team. We\u2019re using React with client-side routing via React Router, and one of the first elements that we made was the main menu. Menus are a key component of any site or app. That\u2019s really\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2018\/12\/menu-button.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":207389,"url":"https:\/\/css-tricks.com\/diy-priority-plus-nav\/","url_meta":{"origin":150992,"position":5},"title":"DIY Priority+ Navigation","date":"August 31, 2015","format":false,"excerpt":"We've written a bit about the Priority+ pattern here before. Here's a bunch of real world examples. I needed to use it recently. I found Gijs Rog\u00e9's priority-navigation. It's pretty nice: no dependencies, clean code, fairly configurable and designable. It's not particularly small though, and there is a good amount\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"featured_media_src_url":null,"_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/150992"}],"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\/248413"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=150992"}],"version-history":[{"count":7,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/150992\/revisions"}],"predecessor-version":[{"id":253682,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/150992\/revisions\/253682"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=150992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=150992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=150992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}