Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1009843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:59:27+00:00 2026-05-16T08:59:27+00:00

Is it possible to select, say, every fourth element in a set of elements?

  • 0

Is it possible to select, say, every fourth element in a set of elements?

Ex: I have 16 <div> elements… I could write something like.

div:nth-child(4),
div:nth-child(8),
div:nth-child(12),
div:nth-child(16)

is there a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:59:28+00:00Added an answer on May 16, 2026 at 8:59 am

    As the name implies, :nth-child() allows you to construct an arithmetic expression using the n variable in addition to constant numbers. You can perform addition (+), subtraction (-) and coefficient multiplication (an where a is an integer, including positive numbers, negative numbers and zero).

    Here’s how you would rewrite the above selector list:

    div:nth-child(4n)
    

    For an explanation on how these arithmetic expressions work, see my answer to this question, as well as the spec.

    Note that this answer assumes that all of the child elements within the same parent element are of the same element type, div. If you have any other elements of different types such as h1 or p, you will need to use :nth-of-type() instead of :nth-child() to ensure you only count div elements:

    <body>
      <h1></h1>
      <div>1</div>  <div>2</div>
      <div>3</div>  <div>4</div>
      <h2></h2>
      <div>5</div>  <div>6</div>
      <div>7</div>  <div>8</div>
      <h2></h2>
      <div>9</div>  <div>10</div>
      <div>11</div> <div>12</div>
      <h2></h2>
      <div>13</div> <div>14</div>
      <div>15</div> <div>16</div>
    </body>
    

    For everything else (classes, attributes, or any combination of these), where you’re looking for the nth child that matches an arbitrary selector, you will not be able to do this with a pure CSS selector. See my answer to this question.


    By the way, there’s not much of a difference between 4n and 4n + 4 with regards to :nth-child(). If you use the n variable, it starts counting at 0. This is what each selector would match:

    :nth-child(4n)

    4(0) = 0
    4(1) = 4
    4(2) = 8
    4(3) = 12
    4(4) = 16
    ...
    

    :nth-child(4n+4)

    4(0) + 4 = 0  + 4 = 4
    4(1) + 4 = 4  + 4 = 8
    4(2) + 4 = 8  + 4 = 12
    4(3) + 4 = 12 + 4 = 16
    4(4) + 4 = 16 + 4 = 20
    ...
    

    As you can see, both selectors will match the same elements as above. In this case, there is no difference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to use XPath to select only the nodes that have a
[UPDATE: MS SQL Server 2005] Hi is it possible to select a bunch of
Using the jQuery UI Tabs component, is it possible to select new tabs on
There are three tables, Students, Classes, Student_Class Is it possible to select students who
How is it possible to programmatically select an item in a WPF TreeView ?
Is it possible to do a SELECT statement with a predetermined order, ie. selecting
Is it possible to use the OpenFileDialog class select a file OR folder? It
Is it possible to create a MySQL select statement that uses an expression as
Is it possible using jQuery to select all <a> links which href ends with
select * from myTable where myInt will not show any possible_keys when explaining the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.