Grow your CSS skills. Land your dream job.

Last updated on:

Insert Element Every nth Loop

When inside of a loop, you can keep track of the iteration number of the loop (shown below is a simple for loop). Using that iteration number, you can calculate it's modulus of some number (number left over after an even division). If that modulus is zero, you are at an even division of whatever that number was.

So in this simple loop below, it will output something every third time through the loop:

   for ($counter = 1; $counter < 100; $counter++ ) {
     if ($counter % 3 == 0) {
            echo "<p>Every third!</p>";


  1. Permalink to comment#

    I had no idea you could do this! There’s me doing if(float/3 == round(float/3)){!

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like this:

<div>Example code</div>

You don't need to escape code in backticks, Markdown does that for you. If anything screws up, contact us and we can fix it up for you.

*May or may not contain any actual "CSS" or "Tricks".