 <p>you should use a <code>table</code> as grid </p> <p>Try this </p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;?php $numbers_per_row = 5; $rowi= 0; $numbers_limit = 25; for($b=1; $b&lt;=$numbers_limit ; $b++) { echo '&lt;td&gt;&lt;a href="index.php?page='.$b.'"&gt;'.$b.'&lt;/td&gt;'; if($rowi&lt;=$numbers_per_row) { $rowi++; } else { echo '&lt;/tr&gt;&lt;tr style="margin-top:10px;"&gt;'; /* &lt;-- change it ^ to what you want */ /* Reset counter */ $rowi = 0; } } ?&gt; &lt;/table&gt; </code></pre>