Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
CSS

CSS: Fix a :nth-child() Selector That Silently Counts Every Sibling Instead of Just the Ones Matching Your Selector

- 15.10.26 - ErcanOPAK

🎨 The nth-child That Counted the Wrong Crowd

:nth-child(n) counts a position among ALL of an element’s siblings, regardless of what selector it’s attached to – li:nth-child(2) doesn’t mean ‘the second li,’ it means ‘this element, if it happens to be an li, counted by its position among every sibling of any kind.’ Mix list items with a non-li element (a divider, a heading, an ad slot) anywhere in between, and the count shifts for every item after it, picking out the wrong element entirely while compiling and rendering with zero errors.

🔎 The Problem

<ul>
  <li>Item A</li>
  <li class="divider"></li>  <!-- not styled with nth-child -->
  <li>Item B</li>
  <li>Item C</li>
</ul>

<style>
  /* Intent: highlight the second REAL item (Item B). */
  li:nth-child(2) { background: yellow; }
</style>

<!-- What actually gets highlighted: the divider, which IS
     the second <li> child by raw position - "Item B" is
     the third child overall, so nth-child(2) never
     selects it, even though visually it's "the second
     real item" to a person reading the list. -->

âś… Fix: Use :nth-of-type, or the of S Selector-List Syntax for a Filtered Count

  • If every sibling in the group is the same tag (all <li>, mixed only with non-element content), :nth-of-type(n) counts by tag name among siblings of that same tag – closer to what most people expect :nth-child to do, but still not a filter by class.
  • For counting by an actual CSS selector – ‘the 2nd li that doesn’t have class divider’ – use the newer :nth-child(2 of li:not(.divider)) selector-list syntax where it’s supported; it’s the direct, purpose-built way to count only the siblings matching an arbitrary selector.
  • Where broad support for the of S syntax isn’t an option yet, the dependable fallback is giving the excluded elements (dividers, ads, headings) a wrapper or a different parent entirely, so the list you’re counting through only ever contains the items you actually mean to count.

⚠️ Why This Is Easy to Miss

  • The selector is valid, specific, and clearly matches something – there’s no error, no unmatched-selector warning, just a highlight landing on an element that isn’t the one anyone was picturing when they wrote ‘2nd item.’
  • It only breaks the moment a non-uniform sibling gets inserted into a previously uniform list – a design built and tested against a clean list of identical items can ship fine, then quietly mis-target the moment a divider or an ad slot gets added later.

:nth-child doesn’t know what selector it’s attached to when it’s counting – it’s already decided which child is ‘the second one’ before it ever checks whether that child matches anything you wrote.

— UI Engineer

Related posts:

CSS: Use Filter Effects for Visual Magic

CSS: Use Filter Effects for Visual Magic

CSS Animations Cause Page Jank

CSS Animations Cause Page Jank

CSS “Text Overflow Ellipsis Not Working” — The Missing Combo

CSS “Text Overflow Ellipsis Not Working” — The Missing Combo

Views: 7

Post navigation

The AI Prompt That Catches Missing Signature Verification in Your Webhook Handler Before It Trusts the Payload
ASP.NET Core: Fix an Authorization Check That Silently Requires Every Role Instead of Any Because of Two Stacked [Authorize] Attributes

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (981)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com