Odd and even row styling helps make tables easier to read by coloring rows differently. It separates information visually like stripes on a zebra.
Odd and even row styling in Tailwind
class="odd:bg-color even:bg-color"Use odd: to style odd-numbered rows and even: for even-numbered rows.
These classes work on elements containing the rows, such as <tbody> or <ul>, not directly on the rows themselves.
<tbody class="odd:bg-gray-100 even:bg-white">...</tbody><ul class="odd:text-blue-600 even:text-red-600"><li>Item</li></ul><div class="odd:bg-green-50 even:bg-green-100"><div>Row</div></div>This example shows a simple user table. Odd rows have a light gray background, and even rows are white. This makes it easier to follow each row across the table.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Odd and Even Row Styling</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <main> <h1 class="text-2xl font-bold mb-4">User List with Odd and Even Row Colors</h1> <table class="w-full border-collapse"> <thead> <tr class="bg-gray-300"> <th class="border p-2 text-left">Name</th> <th class="border p-2 text-left">Email</th> <th class="border p-2 text-left">Role</th> </tr> </thead> <tbody class="odd:bg-gray-100 even:bg-white"> <tr> <td class="border p-2">Alice</td> <td class="border p-2">alice@example.com</td> <td class="border p-2">Admin</td> </tr> <tr> <td class="border p-2">Bob</td> <td class="border p-2">bob@example.com</td> <td class="border p-2">User</td> </tr> <tr> <td class="border p-2">Carol</td> <td class="border p-2">carol@example.com</td> <td class="border p-2">Editor</td> </tr> <tr> <td class="border p-2">Dave</td> <td class="border p-2">dave@example.com</td> <td class="border p-2">User</td> </tr> </tbody> </table> </main> </body> </html>
Make sure your parent container (like <tbody>) wraps the rows for odd/even styling to work.
You can combine odd/even with other Tailwind utilities for text, borders, or spacing.
Test your colors for good contrast to keep your table accessible.
Odd and even row styling helps separate rows visually for easier reading.
Use odd: and even: prefixes in Tailwind to style rows differently.
This technique works well for tables, lists, and repeated content blocks.