 <p>You could instead output the images as a list of divs, or just divs, and then use CSS to show the images in two columns. Your layout should not be that hardwired.</p> <pre><code>&lt;style&gt; div.gallery { width: 650px; } div.gallery ul li { list-style: none; float: left; } div.image { height: 500px; width: 300px; } &lt;/style&gt; &lt;div class="gallery"&gt; &lt;ul&gt; &lt;li&gt; &lt;div class="image"&gt; &lt;span class="image_title"&gt;Some title&lt;/span&gt;&lt;br/&gt; &lt;img src="foo.png"/&gt; &lt;/div&gt; &lt;/li&gt; &lt;li&gt; &lt;div class="image"&gt; &lt;span class="image_title"&gt;Another title&lt;/span&gt;&lt;br/&gt; &lt;img src="bar.png"/&gt; &lt;/div&gt; &lt;/li&gt; &lt;li&gt; &lt;div class="image"&gt; &lt;span class="image_title"&gt;Another title&lt;/span&gt;&lt;br/&gt; &lt;img src="foo.png"/&gt; &lt;/div&gt; &lt;/li&gt; &lt;li&gt; &lt;div class="image"&gt; &lt;span class="image_title"&gt;Another title&lt;/span&gt;&lt;br/&gt; &lt;img src="bar.png"/&gt; &lt;/div&gt; &lt;/li&gt; &lt;li&gt; &lt;div class="image"&gt; &lt;span class="image_title"&gt;Another title&lt;/span&gt;&lt;br/&gt; &lt;img src="foo.png"/&gt; &lt;/div&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>Result:</p> <p><img src="https://i.stack.imgur.com/kk8QZ.png" alt="enter image description here"></p> <p>Your code should look something like:</p> <pre><code>&lt;div class="gallery"&gt; &lt;ul&gt; &lt;?php include_once("config.php"); $result = mysql_query("SELECT * FROM images"); while($res = mysql_fetch_array($result)) { ?&gt; &lt;li&gt; &lt;div class="image"&gt; &lt;a class="image_title" href="indimage.php?imageid=&lt;?php echo $res['imageid']?&gt;"&gt;&lt;?php echo $res['imagename']?&gt;&lt;/a&gt;&lt;br/&gt; &lt;a href="indimage.php?imageid=&lt;?php echo $res['imageid']?&gt;"&gt;&lt;img src="&lt;?php echo $res['image']?&gt;" /&gt;&lt;/a&gt; &lt;/div&gt; &lt;/li&gt; &lt;?php } ?&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre>