Yes, you can use the nth-child selector group, as in the snippet below, to select a repeating group of elements based on a pattern.
It should be noted that the selection of each 4th element and the next 4 after it is equivalent to the selection of all elements after the 4th element, and therefore I limited the selection to only the following two elements.
Selector explanation (selects 4, 5, 8, 9 elements, etc.):
nth-child(4n+4) - selects the 4th (4 * 0 + 4), 8th (4 * 1 + 4), 12th (4 * 2 +4) elementsnth-child(4n+5) - selects the elements of the 5th (4 * 0 + 5), 9th (4 * 1 + 5), 13th (4 * 2 + 5).
As you can see from the explanation, the series starts from the 4th element and repeats from now on. If you need to start with a series of the 1st element (e.g. 1st, 2nd, 5th, 6th, etc.), you can use the selector group as div:nth-child(4n+1), div:nth-child(4n+2) .
div:nth-child(4n+4), div:nth-child(4n+5){ color: red; }
<div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> <div>13</div> <div>14</div> <div>15</div> <div>16</div>
Selector explanation (selects 4th, 5th, 12th, 13th elements, etc.):
nth-child(8n+4) - selects the 4th (8 * 0 + 4), 12th (8 * 1 + 4), 20th (8 * 2 + 4) elementsnth-child(8n+5) - selects the 5th (8 * 0 + 5), 13th (8 * 1 + 5), 21th (8 * 2 + 5) elements.
As you can see from the explanation, the series starts from the 4th element and repeats from now on. If you need to start with a series of the 1st element (e.g. 1st, 2nd, 5th, 6th, etc.), you can use the selector group as div:nth-child(4n+1), div:nth-child(4n+2) .
div:nth-child(8n+4), div:nth-child(8n+5){ color: red; }
<div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> <div>10</div> <div>11</div> <div>12</div> <div>13</div> <div>14</div> <div>15</div> <div>16</div> <div>17</div> <div>18</div> <div>19</div> <div>20</div> <div>21</div> <div>22</div> <div>23</div>
Harry
source share