{"id":1024,"date":"2026-12-05T15:00:03","date_gmt":"2026-12-05T15:00:03","guid":{"rendered":"https:\/\/html-editor-online.com\/blog?p=1024"},"modified":"2026-12-05T15:00:03","modified_gmt":"2026-12-05T15:00:03","slug":"css-selectors-eq-nth-element","status":"publish","type":"post","link":"https:\/\/html-editor-online.com\/blog\/css-selectors-eq-nth-element\/","title":{"rendered":"CSS Selectors eq: How to Pick the Nth Element Without jQuery&#8217;s :eq()"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If you are searching for css selectors eq, the short answer is that CSS has no <code>:eq()<\/code> pseudo-class. jQuery&#8217;s <code>:eq(1)<\/code> picks the second element of a matched set using a zero-based index, while CSS <code>:nth-child(2)<\/code> uses a one-based position among element siblings. For a single plain list, add 1 to the jQuery index and you get the CSS number.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This article compares <code>:nth-child()<\/code> and <code>:nth-of-type()<\/code>, shows how to combine a class and a position in one compound selector, gives you a small list to test in the HTML Code Editor and CSS Editor, and explains when <code>querySelectorAll()<\/code> with an index is the better tool. The input is HTML markup plus CSS rules, and the output is a visible highlight you can check by eye.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li class=\"\"><a href=\"#css-selectors-eq-why-jquery-counts-from-0-and-css-counts-from-1-1\">CSS selectors eq: why jQuery counts from 0 and CSS counts from 1<\/a><\/li><li class=\"\"><a href=\"#nth-child-vs-nth-of-type-what-each-selector-counts-2\">:nth-child() vs :nth-of-type(): what each selector counts<\/a><\/li><li class=\"\"><a href=\"#css-selectors-eq-with-a-class-compound-and-versus-filter-first-3\">CSS selectors eq with a class: compound AND versus filter-first<\/a><\/li><li class=\"\"><a href=\"#test-the-demo-in-the-html-code-editor-and-css-editor-4\">Test the demo in the HTML Code Editor and CSS Editor<\/a><\/li><li class=\"\"><a href=\"#when-queryselectorall-with-an-index-is-the-better-choice-5\">When querySelectorAll with an index is the better choice<\/a><\/li><li class=\"\"><a href=\"#check-the-result-and-avoid-common-mistakes-6\">Check the result and avoid common mistakes<\/a><\/li><li class=\"\"><a href=\"#frequently-asked-questions\">Frequently asked questions<\/a><\/li><li class=\"\"><a href=\"#next-steps\">Next steps<\/a><\/li><li class=\"\"><a href=\"#sources\">Sources and further reading<\/a><\/li><\/ul><\/nav><\/div>\n\n\n<div class=\"wp-block-image heo-auto-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-1-968-1-575d5ddc82865edb-1024x576.jpg\" alt=\"Two rulers with different starting points beside a row of blocks, illustrating zero-based and one-based counting\" class=\"wp-image-1028\" srcset=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-1-968-1-575d5ddc82865edb-1024x576.jpg 1024w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-1-968-1-575d5ddc82865edb-300x169.jpg 300w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-1-968-1-575d5ddc82865edb-768x432.jpg 768w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-1-968-1-575d5ddc82865edb.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"css-selectors-eq-why-jquery-counts-from-0-and-css-counts-from-1-1\">CSS selectors eq: why jQuery counts from 0 and CSS counts from 1<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">jQuery&#8217;s position selectors work on a matched set. In <code>$('li.featured:eq(1)')<\/code>, jQuery first collects every <code>li.featured<\/code> in document order and then returns the item at index 1, which is the second one. Indexes start at 0, so <code>:eq(0)<\/code> is the first element. The official <a href=\"https:\/\/api.jquery.com\/eq-selector\/\" title=\":eq() Selector | jQuery API Documentation\" target=\"_blank\" rel=\"noopener\">jQuery API documentation<\/a> marks the selector deprecated as of jQuery 3.4 and recommends the separate <code>.eq()<\/code> method for code that still uses jQuery (checked November 30, 2026).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CSS works differently. A selector is tested against each element on its own, and <code>:nth-child(2)<\/code> asks one question: is this element the second element child of its parent? The count starts at 1, and the answer can be yes for several parents at once. That gives you two rules to remember.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Add 1 to a nonnegative jQuery index only when the CSS rule counts the same elements in the same order.<\/li>\n\n\n\n<li>CSS can match once per parent, while <code>:eq()<\/code> returns one element from the whole matched set.<\/li>\n\n\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>jQuery index<\/th><th>CSS position<\/th><th>Equivalent only when<\/th><\/tr><\/thead><tbody><tr><td><code>:eq(0)<\/code><\/td><td><code>:nth-child(1)<\/code><\/td><td>The page has one list and the items are all its element children<\/td><\/tr><tr><td><code>:eq(1)<\/code><\/td><td><code>:nth-child(2)<\/code><\/td><td>The same condition holds<\/td><\/tr><tr><td><code>:eq(2)<\/code><\/td><td><code>:nth-of-type(3)<\/code><\/td><td>The page has one parent and you want the third item of that tag<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">jQuery also accepts negative indexes that count from the end. That is a separate case: the CSS counterpart is <code>:nth-last-child()<\/code> or <code>:last-child<\/code>, not a shift by 1.<\/p>\n\n\n<div class=\"wp-block-image heo-auto-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-2-968-2-f49d6cde0ded3f36-1024x576.jpg\" alt=\"Blocks on a shelf where one highlight counts every block and another counts only matching blocks\" class=\"wp-image-1030\" srcset=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-2-968-2-f49d6cde0ded3f36-1024x576.jpg 1024w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-2-968-2-f49d6cde0ded3f36-300x169.jpg 300w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-2-968-2-f49d6cde0ded3f36-768x432.jpg 768w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-2-968-2-f49d6cde0ded3f36.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"nth-child-vs-nth-of-type-what-each-selector-counts-2\">:nth-child() vs :nth-of-type(): what each selector counts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The two selectors count different things, and the difference only shows when siblings are not all the same tag. <code>li:nth-child(2)<\/code> matches an <code>li<\/code> only if it is its parent&#8217;s second element child, whatever the first child&#8217;s tag is. <code>li:nth-of-type(2)<\/code> matches the second <code>li<\/code> among its siblings and ignores siblings of other types. Neither counts text nodes or comments, and neither filters by class before counting. These counting rules are specified in <a href=\"https:\/\/www.w3.org\/TR\/2026\/WD-selectors-4-20260122\/\" title=\"Selectors Level 4\" target=\"_blank\" rel=\"noopener\">Selectors Level 4<\/a>, a Working Draft dated January 22, 2026.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A list of identical items hides the difference, so use a container that starts with a heading:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;div class=&quot;box&quot;&gt;\n  &lt;h2&gt;Heading&lt;\/h2&gt;\n  &lt;p&gt;First paragraph&lt;\/p&gt;\n  &lt;p&gt;Second paragraph&lt;\/p&gt;\n&lt;\/div&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here <code>p:nth-child(2)<\/code> selects the first paragraph, because that paragraph is the second child of the div. <code>p:nth-of-type(2)<\/code> selects the second paragraph, because it is the second <code>p<\/code> among its siblings. The heading takes position 1 when counting children but is ignored when counting paragraphs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you port a jQuery selector, ask which behavior the old code meant. If it was <code>$('.box p:eq(1)')<\/code>, the intent was the second paragraph, so <code>:nth-of-type(2)<\/code> is the closer match. If the structure guarantees only one kind of child, both selectors give the same result. I reach for <code>:nth-of-type()<\/code> when the tag matters and <code>:nth-child()<\/code> when the slot in the layout matters.<\/p>\n\n\n<div class=\"wp-block-image heo-auto-image\">\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;6b14ba9f2fc1e&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"6b14ba9f2fc1e\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on--pointerdown=\"actions.preloadImage\" data-wp-on--pointerenter=\"actions.preloadImageWithDelay\" data-wp-on--pointerleave=\"actions.cancelPreload\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-3-968-3-2ce622e6de4b69d3-1024x576.jpg\" alt=\"Tokens passing through two gates, illustrating a compound AND selector versus filtering before counting\" class=\"wp-image-1032\" srcset=\"https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-3-968-3-2ce622e6de4b69d3-1024x576.jpg 1024w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-3-968-3-2ce622e6de4b69d3-300x169.jpg 300w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-3-968-3-2ce622e6de4b69d3-768x432.jpg 768w, https:\/\/html-editor-online.com\/blog\/wp-content\/uploads\/2026\/11\/css-selectors-eq-image-3-968-3-2ce622e6de4b69d3.jpg 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\tdata-wp-bind--aria-label=\"state.thisImage.triggerButtonAriaLabel\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.thisImage.buttonRight\"\n\t\t\tdata-wp-style--top=\"state.thisImage.buttonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"css-selectors-eq-with-a-class-compound-and-versus-filter-first-3\">CSS selectors eq with a class: compound AND versus filter-first<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">jQuery lets you write <code>li.featured:eq(1)<\/code> and means the second featured item. In CSS, the way to say AND is a compound selector, with the conditions written together without spaces. <code>li.featured:nth-child(2)<\/code> matches an element that is an <code>li<\/code>, has the class <code>featured<\/code>, and is the second element child. All three conditions must be true of the same element. That is the AND CSS selector pattern.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/html-editor-online.com\/blog\/css-class-selector-vs-id-selector\/\" title=\"CSS Class Selector vs Id Selector\">CSS class selector vs ID selector guide<\/a> shows the same no-space rule for two classes on one element. A space changes the meaning completely: <code>.featured :nth-child(2)<\/code> targets a second-child descendant inside a <code>.featured<\/code> element, which is rarely what you want here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is one catch. A compound selector tests the position as a fact about the element and the class as another fact. It does not filter by class and then count. If the second child is not featured, <code>li.featured:nth-child(2)<\/code> does not move on to the next featured item. To count only featured siblings, use the <code>of<\/code> clause:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">li.featured:nth-child(2) { font-weight: bold; }\n.featured :nth-child(2) { color: green; }\n:nth-child(2 of li.featured) { outline: 2px solid rebeccapurple; }<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>li:nth-child(2)<\/code> is the second element child, with any class.<\/li>\n\n\n\n<li><code>li.featured:nth-child(2)<\/code> is the second element child, but only if it is a featured <code>li<\/code>.<\/li>\n\n\n\n<li><code>:nth-child(2 of li.featured)<\/code> is the second featured <code>li<\/code> among its siblings, wherever it sits.<\/li>\n\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Compatibility, checked November 30, 2026: the <a href=\"https:\/\/web-platform-dx.github.io\/web-features-explorer\/features\/nth-child-of\/\" title=\"Web features explorer - :nth-child() of\" target=\"_blank\" rel=\"noopener\">Web Features Explorer<\/a> lists the <code>of<\/code> syntax as Widely Available since November 9, 2025, with support starting in Chrome 111 and Edge 111 (March 2023), Firefox 113 (May 2023) and Safari 9 (September 2015). Those minimum versions apply to the <code>of<\/code> clause, not to basic <code>:nth-child()<\/code>. The <code>of<\/code> form still counts per parent, so it is not a perfect copy of jQuery&#8217;s whole-set behavior.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"test-the-demo-in-the-html-code-editor-and-css-editor-4\">Test the demo in the HTML Code Editor and CSS Editor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use the <a href=\"https:\/\/html-editor-online.com\/editors\/html\" title=\"HTML Code Editor\">HTML Code Editor<\/a> to test exact markup, because it has a sandboxed live preview that renders your page. Paste the document below into it. The rules sit in a style block, so the CSS and the two lists live together.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;style&gt;\n  .demo &gt; li:nth-child(2) { text-decoration: underline; }\n  .demo &gt; li.featured:nth-child(2) { font-weight: bold; }\n  .demo &gt; :nth-child(2 of li.featured) { outline: 2px solid rebeccapurple; }\n&lt;\/style&gt;\n\n&lt;ul class=&quot;demo&quot;&gt;\n  &lt;li class=&quot;featured&quot;&gt;Apple&lt;\/li&gt;\n  &lt;li&gt;Banana&lt;\/li&gt;\n  &lt;li class=&quot;featured&quot;&gt;Cherry&lt;\/li&gt;\n&lt;\/ul&gt;\n\n&lt;ul class=&quot;demo&quot;&gt;\n  &lt;li class=&quot;featured&quot;&gt;Fig&lt;\/li&gt;\n  &lt;li class=&quot;featured&quot;&gt;Grape&lt;\/li&gt;\n&lt;\/ul&gt;<\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Look at the preview. Banana and Grape are underlined, only Grape is bold, and Cherry and Grape are outlined.<\/li>\n\n\n\n<li>Replace <code>:nth-child(2)<\/code> in the first rule with <code>:nth-of-type(2)<\/code>. Nothing changes, because every element child in these lists is an <code>li<\/code>.<\/li>\n\n\n\n<li>Remove the <code>featured<\/code> class from Grape. The bold and the outline disappear, but the underline stays, because the first rule ignores classes.<\/li>\n\n\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/html-editor-online.com\/editors\/css\" title=\"CSS Editor\">CSS Editor<\/a> previews your stylesheet on a fixed sample page rather than on your own markup, so pasting the <code>.demo<\/code> rules there will not highlight anything. Use it to write and edit stylesheet rules with syntax highlighting and property auto-completion, and to see their effect on the sample page. To test how a selector counts elements in your own lists, keep using the HTML Code Editor with the markup above.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When your rules grow, the <a href=\"https:\/\/html-editor-online.com\/tools\/css_beautifier\" title=\"CSS Beautifier\">CSS Beautifier<\/a> can tidy the indentation and spacing. Beautifying only changes layout. It does not check that a selector matches what you intended.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"when-queryselectorall-with-an-index-is-the-better-choice-5\">When querySelectorAll with an index is the better choice<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CSS selectors match elements, but they do not pick one result out of a combined list. If you need exactly one element from the matches across all parents, need the element in a script, or calculate the index at runtime, JavaScript is the closer replacement for jQuery&#8217;s <code>:eq()<\/code>. The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/querySelectorAll\" title=\"Document: querySelectorAll() method - Web APIs | MDN\" target=\"_blank\" rel=\"noopener\">MDN reference for querySelectorAll()<\/a> describes a list returned in document order, and ordinary bracket indexing is zero-based, just like jQuery.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const item = document.querySelectorAll(&#039;.demo &gt; li.featured&#039;)&#091;1&#093;;\nitem?.classList.add(&#039;picked&#039;);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the demo, the featured items in document order are Apple, Cherry, Fig and Grape, so index 1 returns Cherry only. The CSS rule with <code>of<\/code> matched Cherry and Grape because it counts per parent. That is the clearest summary of the difference: use CSS when every parent should be styled, and use script when one element across the whole set is the target.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>querySelectorAll()<\/code> returns a static <code>NodeList<\/code>. Query again if the DOM changes and you need a fresh result.<\/li>\n\n\n\n<li>An out-of-range index returns <code>undefined<\/code>, so guard it, for example with the optional chaining shown above.<\/li>\n\n\n\n<li>Never pass <code>:eq()<\/code> to <code>querySelectorAll()<\/code>. It is not valid CSS selector syntax and throws a <code>SyntaxError<\/code>. If you keep jQuery, use the <code>.eq()<\/code> method instead.<\/li>\n\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To practice the indexing logic on plain arrays and see synchronous console output, the <a href=\"https:\/\/html-editor-online.com\/editors\/javascript\" title=\"JavaScript Editor\">JavaScript Editor<\/a> runs code in the browser. It runs in the editor page, so a DOM query only finds elements that exist in that page. Run only code you trust.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"check-the-result-and-avoid-common-mistakes-6\">Check the result and avoid common mistakes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Most problems with a ported selector come from a handful of causes. Check these first.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Forgetting the shift from a zero-based to a one-based number.<\/li>\n\n\n\n<li>Using <code>:nth-child()<\/code> when siblings have mixed tags and the tag was what you meant to count.<\/li>\n\n\n\n<li>Adding a space inside what should be a compound selector, which turns it into a descendant selector.<\/li>\n\n\n\n<li>Expecting a class to filter siblings before counting, when only the <code>of<\/code> clause does that.<\/li>\n\n\n\n<li>Expecting a single match when the rule matches once per parent.<\/li>\n\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To verify, change one thing at a time and use a visible style such as an outline, so you can count the highlighted elements in the preview. If a rule matches but a style does not apply, the cause may be specificity. A pseudo-class such as <code>:nth-child()<\/code> adds weight like a class does, and the guide on <a href=\"https:\/\/html-editor-online.com\/blog\/css-where-specificity-rule-not-applying\/\" title=\"CSS Where Specificity Rule Not Applying\">why a CSS rule inside :where() loses to a plain element selector<\/a> walks through that kind of check.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the checks separate. A clean preview shows that the rule works in the browser you used. It does not prove support in older browsers, and it does not prove the selector will still match if the markup changes. When the HTML is generated, re-test with the real markup.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"frequently-asked-questions\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-1\">Is there an :eq() selector in CSS?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. CSS has no :eq() pseudo-class. It is a jQuery extension, and jQuery&#8217;s documentation marks it deprecated as of version 3.4. In plain CSS you use :nth-child(), :nth-of-type() or the of clause, and in JavaScript you index the result of querySelectorAll().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-2\">What is the CSS equivalent of jQuery&#039;s :eq()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For a single list of same-tag siblings, :eq(n) matches :nth-child(n + 1), because CSS counts from 1. It is not an exact replacement, since CSS can match once per parent. Use querySelectorAll() with an index for one element across the whole set.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-3\">What is the difference between :nth-child() and :nth-of-type()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">:nth-child(2) matches an element only if it is the second element child of its parent, whatever its tag. :nth-of-type(2) matches the second element of its own tag among siblings. They differ when siblings have mixed tags, such as a heading followed by paragraphs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-4\">How do I write an AND CSS selector with a class and a position?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Write the conditions together without spaces, for example li.featured:nth-child(2). The same element must be an li, have the class featured, and be the second element child. A space would make it a descendant selector, which has a different meaning.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-5\">How do I select the second element with a class in CSS?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use :nth-child(2 of li.featured) to pick the second featured li among its siblings. It filters by class before counting. The compound li.featured:nth-child(2) instead requires the element to be the second child overall and also have the class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-6\">Why does querySelectorAll(&#039;li:eq(1)&#039;) throw an error?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Because :eq() is not valid CSS selector syntax, so the browser rejects it with a SyntaxError. Query with a standard selector such as li.featured, then take the item at index 1 from the returned list, and guard against an undefined result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"faq-7\">Does :nth-child() count text nodes or comments?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. It counts only element siblings, so text nodes and comments are ignored. It also does not filter by class before counting, which is why a compound selector like li.featured:nth-child(2) tests position and class as two separate conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"next-steps\">Next steps<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The css selectors eq answer comes down to three habits: remember that CSS counts from 1, choose between <code>:nth-child()<\/code> and <code>:nth-of-type()<\/code> by what you want counted, and use a compound selector or the <code>of<\/code> clause when a class is part of the condition. Reach for <code>querySelectorAll()<\/code> with an index when you need a single element from the whole set. As a next step, paste the demo list into the HTML Code Editor, swap the positions and classes, and watch which elements change.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Related tools and resources on HTML Editor Online:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/html-editor-online.com\/blog\/css-class-selector-vs-id-selector\/\">CSS Class Selector vs Id Selector<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/blog\/visual-css-editor-browser-live-preview\/\">Visual CSS Editor Browser Live Preview<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/editors\/css\">CSS Editor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/editors\/html\">HTML Code Editor<\/a><\/li>\n\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sources\">Sources and further reading<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.jquery.com\/eq-selector\/\" target=\"_blank\" rel=\"noopener\">:eq() Selector | jQuery API Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/querySelectorAll\" target=\"_blank\" rel=\"noopener\">Document: querySelectorAll() method &#8211; Web APIs | MDN<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.w3.org\/TR\/2026\/WD-selectors-4-20260122\/\" target=\"_blank\" rel=\"noopener\">Selectors Level 4<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/editors\/html\">HTML Code Editor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/html-editor-online.com\/editors\/css\">CSS Editor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/web-platform-dx.github.io\/web-features-explorer\/features\/nth-child-of\/\" target=\"_blank\" rel=\"noopener\">Web features explorer &#8211; :nth-child() of<\/a><\/li>\n\n\n<\/ul>\n\n\n","protected":false},"excerpt":{"rendered":"<p>CSS selectors eq has no direct match, but :nth-child() and :nth-of-type() cover it. Learn the index shift, compound AND rules and when JavaScript is better.<\/p>\n","protected":false},"author":1,"featured_media":1027,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[347,343,69,345,334,344,346],"class_list":["post-1024","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css-editor","tag-compound-selector","tag-css-pseudo-classes","tag-css-selectors","tag-jquery-eq","tag-nth-child","tag-nth-of-type","tag-queryselectorall"],"_links":{"self":[{"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/posts\/1024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/comments?post=1024"}],"version-history":[{"count":5,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/posts\/1024\/revisions"}],"predecessor-version":[{"id":1033,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/posts\/1024\/revisions\/1033"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/media\/1027"}],"wp:attachment":[{"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/media?parent=1024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/categories?post=1024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/html-editor-online.com\/blog\/wp-json\/wp\/v2\/tags?post=1024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}