I would like to change the appearance of the categories and archives widgets in the sidebar such that each category/month alternates the row color. The only way I know how to accomplish this requires modifying the PHP code in the core that generates the category and archives widgets (outputting alternating css styles depending on odd/even rows). I want to avoid modifying core files and instead do everything in the theme. Is there a way to accomplish this even/odd row styling for these widgets from within the theme or via CSS only?
2 Answers
Use the :nth-child()
pseudo-class:
li:nth-child(odd){
background-color: white;
}
li:nth-child(even){
background-color: green;
}
The only way I know how to accomplish this requires modifying the PHP code in the core that generates the category and archives widgets (outputting alternating css styles depending on odd/even rows).
If you can't do it in CSS, don't edit the core. Copy the core widget's code and create your own widget.
By the way, I really like One Trick Pony's solution (+1) but it is a CSS3 solution which I still do not trust. ( A quick look around suggests that IE8 does not support nth-child
) We are getting close to acceptable support though. I am waiting for the day...