[SalesForce] alternating row colors in a VF table rendered as PDF

I need to alternate row colors in a particular way:

The headers is in one color (let's say yellow).

The first, second row (which is actually splitted in two rows at some point) and the third row is in one color (blue)

The fourth, fith (which again splits in two rows at some point) and sixth row should be in a differnet color. (red)

And from here it repeats alternativately. How can I do this? I would appreciate any sample code. Thank you very much!

Best Answer

Using CSS, you can specify :nth-child(odd) or :nth-child(even) in order to alternate colors per row. This is compatible with the PDF renderer.

<style>
table tr:nth-child(even) {
    background: #ccc;
}
table tr:nth-child(odd) {
    background: #aaa;
}
</style>