I am facing aProblem.I want to show a table having row with alternative background color.i am displaying data using .I cant use pageblocktable or any other table method .I have to complete my problem with apex repeat only . Please anyone Suggest?


The CSS approach is the best way to go. But if for some reason you can't do that, be aware that there is an apex:variable tag that you can use for things like loop counters or this sort of logic.

Example controller:

public class A {
    public String[] values {
        get {
            return new String[] {'aaaa', 'bbbb', 'cccc', 'dddd'};

and page with alternating color table rows:

<apex:page controller="A">
    <apex:variable var="color" value="red"/>
    <apex:repeat value="{!values}" var="value">
        <tr style="background-color: {!color}"><td>{!value}</td></tr>
        <apex:variable var="color" value="{!IF(color == 'red', 'blue', 'red')}"/>
  • 2
    The only downside here is the bloated view state resulting from the many expressions that will be evaluated as a result of this loop. In fact, the iterator tags consume the most view state per use compared to any other element. However, this certainly does work in a pinch. – sfdcfox Dec 12 '14 at 16:10

You just need to use the CSS Styling like below. Nothing else is required to use along with <apex:repeat>

tr:nth-child(even) {
    background-color: red;
tr:nth-child(odd) {
    background-color: blue;
  • ya this is a solution , but valid only for some limited browser ... – RINKU PAWRIYA Dec 26 '14 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.