A Web Design Community curated by Chris Coyier

Code Snippets Gallery

Code Snippets > PHP > Applying Even/Odd Classes Submit one!

Applying Even/Odd Classes

<div class="example-class<?php echo ($xyz++%2); ?>">

Used inside a loop, class names would be named .example-class0 and .example-class1 alternating. Increasing the “2″ number allows you to increment in thirds or fourths or whatever you need:

class="class<?php echo ($xyz++%3); ?>"

Classes: class0 class1 class2 class0 class1 class2

Change number to 4, this is the result: class0 class1 class2 class3 class0 class1 class2 class3

7 Responses

  1. Laurie says:

    FYI, the 2nd example should read:

    class="class"

  2. Laurie says:

    doh:
    class="class\"

  3. Martin says:

    Easiest method I have ever seen, thank you!

  4. Dian says:

    That was easy!

    Thanks~!

  5. Weldo says:

    Wow. That rocks! Thank you so much!

  6. ayesha says:

    thanku :) heaps :)

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.