Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Easy control to detect every nth list item in Silverstripe

Go to End



Community Member, 10 Posts

3 February 2012 at 10:58pm

Thought I'd post this in case it's of use to anyone looking the apply custom styles to certain list elements.

I had a gallery page with images contained in a ul list with 10px padding on the right and bottom edges. I needed to apply a CSS style on every 5th image (at the right edge of the page) with no padding on the right edge, so it fits the page width structure.

I've used the following jQuery script on sites I've built outside of the Silverstripe framework and it works well, however I was struggling to get this to work correctly with the SS framework.

jQuery(document).ready(function() {
jQuery(".examples li:nth-child(3n)").addClass("nopadding");

There are some $this->iteratorPos % 3 commands outlined on the forums for applying custom styles but this was by far the simplest method in 2.4… MultipleOf() :

<% if MultipleOf(4) %>
<li class="fourth">Your content here</li>
<% else %>
<li>Your content here</li>
<% end_if %>

Thanks to Socks for his post here: