2952 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 200+ members
  • Has sold $125,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+10 more
DDStudios says

I want to target these element for my 3 collumn portfolio page and I did something like this :

jQuery('.page-template-portfolio-full-php #portfolioItem li:nth-child(3n)').css({ marginRight: '0px'});
        jQuery('.page-template-portfolio-full-php #portfolioItem li:nth-child(6n)').css({ marginRight: '0px'});
         jQuery('.page-template-portfolio-full-php #portfolioItem li:nth-child(9n)').css({ marginRight: '0px'});
          jQuery('.page-template-portfolio-full-php #portfolioItem li:nth-child(12n)').css({ marginRight: '0px'});

I know it’s not the best way and I know I could do something like i++ or something but im not sure exactly what it is ..

I want to do the same thing for the 2 collumn .. only this time target every 2nd, 4th, 6th element etc etc ..

How is it done?

Thanks guys!

4390 posts ThemeForest Reviewer
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Member of the Envato Team
+12 more
Ivor Envato team says

There you go.

jQuery('#portfolioItem li:nth-child(3n+3)').css({ marginRight: '0px' });
181 posts
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+3 more
MCStudios says

Hi DD

I think it’s easier with i++ you can do it this way:

$counter =0;

query_posts  <--- your normal query

if ( have_posts() )  ,etc. you know the rest

++$counter;
      if($counter == 3) {
        $postclass = 'last';
        $counter = 0;
      } else { $postclass = ''; }
<li class="&lt;?php echo $postclass; ?&gt;">
       your code.....
</li>

This will add the class last to every 3rd, 6th, 9th, etc elements. and then you just need your css.

.last{margin-right: 0 !important;}

Edit: Ivor was faster :)

569 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Had an item featured in a magazine
  • Made it to the Authors' Hall of Fame
+17 more
theMOLITOR says
4390 posts ThemeForest Reviewer
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Member of the Envato Team
+12 more
Ivor Envato team says

Hi DD

I think it’s easier with i++ you can do it this way:

$counter =0;

query_posts  <--- your normal query

if ( have_posts() )  ,etc. you know the rest

++$counter;
      if($counter == 3) {
        $postclass = 'last';
        $counter = 0;
      } else { $postclass = ''; }
<li class="&lt;?php echo $postclass; ?&gt;">
       your code.....
</li>

This will add the class last to every 3rd, 6th, 9th, etc elements. and then you just need your css.

.last{margin-right: 0 !important;}

Edit: Ivor was faster :)

Mine is a single line :D

2952 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 200+ members
  • Has sold $125,000+ on Envato Market
  • Had an item featured in an Envato Bundle
+10 more
DDStudios says

Damn, I knew it was simple :)

Thanks my friend.

181 posts
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+3 more
MCStudios says

Mine is a single line :D

Man i need to learn more about jQuery :)

4390 posts ThemeForest Reviewer
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Member of the Envato Team
+12 more
Ivor Envato team says


Mine is a single line :D
Man i need to learn more about jQuery :)

But your solution gave me an idea for a problem I’m having here haha! Thanks.

611 posts
  • Member of the Envato Team
  • Had an item featured in an Envato Bundle
  • Made it to the Authors' Hall of Fame
  • Contributed a blog post
+10 more
Siddharth Envato team says

Yep, the nth child selector is the way to go. But keep in mind that this take a performance hit if the number of portfolio items is huge. [Actually huge.]

In those cases, you’re better of checking the position of an entry and assigning an appropriate class through server side code, PHP in this case, I’m assuming. Then you can merely use the class selector to manipulate specific elements in the front end, as needed.

This way, computation is offloaded to the server instead of the client having to do the job. Again, this only applies if you have a huge number of elements to parse. Otherwise, the simpler method noted above should do.

2196 posts
  • Located in Europe
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
  • Participated in a focus group or interview to improve the user experience on Envato Market
+10 more
revaxarts says
ok, here comes mine:
jQuery('#portfolioItem').find('li').each(function(i,e){
   if(!i%3) $(e).css({ marginRight: '0px' });
});

The child selector isn’t good for large list. The id selecter is the fastest and the find method is fast too.

Far better is to assign a class which is slightly faster than the css:

jQuery('#portfolioItem').find('li').each(function(i,e){
   if(!i%3) $(e).addClass('last');
});

Anyway, I recommend the latest jquery version but at least 1.4.4.

Edit: and in one line:
jQuery('#portfolioItem').find('li').each(function(i,e){if(!i%3)$(e).addClass('last');});
;)

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by