Progressive Enhancement Grid?

I’m not sure how to best approach this. I have a grid of items and media queries for viewing resolution. I’m thinking this has been dealt with before. Maybe there’s some code somewhere but I’m not sure how to best handle it.

It’s a while loop populating with entries. I adjust the margins to position it. So with different viewing sizes, the margins should change accordingly.

	if ($n == 1) {
		$saved_ids[] = get_the_ID(); ?>
		<figure class="first-entry"><?php the_title(); ?></figure>
	<?php } elseif ($n == 2)  {
		$saved_ids[] = get_the_ID(); ?>
		<figure class="second-entry"><?php the_title(); ?></figure>
	<?php } elseif ($n == 3)  {
		$saved_ids[] = get_the_ID(); ?>
		<figure class="third-entry"><?php the_title(); ?></figure>	
	<?php } elseif ($n == 4)  {
		$saved_ids[] = get_the_ID(); ?>
		<figure class="fourth-entry"><?php the_title(); ?></figure>	
	<?php } elseif ($n == 5)  {
		$saved_ids[] = get_the_ID(); ?>
		<figure class="fifth-entry"><?php the_title(); ?></figure>

Not sure how to best address this. Basically I need a row of 5 items followed by a row of 4 and so forth.