Polish language camps in the UK? Justyna, read this. ►

◄ Windows text editors / Terminus Code custom font

2005-09-27 📌 Solving vertical align issues with style sheets and images

Tags 🏷 All 🏷 Tech

[demonstration of horizontally and vertically centred thumbnails]Vertically aligned thumbnails in DIV containers with block links

The vertical-align CSS property isn't well-supported, nor is display: table-cell ... if you've run into this whilst trying to ditch a table based design for an image gallery, you'll probably know how frustrating it can be.

This solution takes advantage of the fact browsers successfully vertically align images relative to each other. There are two images contained within each link; the actual thumbnail image (which can be portrait or landscape) and another to force the height of the DIV and give us something to position the thumbnail relative to.

In this case I've chosen 110 x 110 links, with thumbnails fitting within 100 x 100 pixels plus 5px of white space, and a 1px border around them to give the impression of a polaroid photo. You could just as easily set a suitable background image instead.

This is a bit of an ugly hack, harkening back to the days of spacer images. However, style sheets allow us to treat the full square area as a clickable link in browsers that support display: block, which is basically everything later than IE 4. There are two further non-obvious style declarations to work around IE bugs that can otherwise lead to doubled left margin and phantom top margin on elements.

Here you go, clean as I could make it, with just two class assignations used...

Whilst ideal for galleries, the limitation is that at least 1px of whitespace is required somewhere in the design for the image element that maintains the height—though you could set a background colour, turn it into a border, etc. What's annoying is that none of this rigmarole should be necessary. You should just be able to specify a height for a containing element, then choose to vertically centre content within it... but this is the real world, with ambiguous markup spec and cruddy browsers from Microsoft.

If you're reading this on the blog, the text above contains an image rather than a code demonstration. You can click here to see it in action or click here for the zipped code.

💬 Comments are off, but you can use the mail form to contact or see the about page for social media links.