So for ages now I have been trying to achieve this. Basically my site allows admin to upload a thumbnail and enlargement image for each item in the catalogue. The thumbnail is displayed on the page the customer sees, and the enlargement is displayed on hover of the thumbnail.
I have no control over the size of the images being uploaded (I suppose I could restrict the width and height, but that wouldn't be practical in this situation).
I would like to both vertically and horizontally centre this image enlargement. It is contained in a div with position: fixed;
. I believe (although I don't have the code in front of me at the moment) that I am currently using something like the following:
.image-enlargement
{
position: fixed;
left: 50%;
top: 50%;
width: 300px;
height: 400px;
margin-left: -150px;
margin-top: -200px;
}
I may have changed it to use jQuery - I can't remember though, it was that long ago when I last worked on this bit of the website. Now what I would like to do with the above code is to take out the width, height and margin specifications so as to allow an image, no matter how big or small, to be exactly centred, and without the use of JavaScript.
Has anyone ever achieved this before? If so, how? I am sure there must be some way to achieve this outcome, but have not come across a pure CSS / HTML solution.
Edit: JSFiddle at http://jsfiddle.net/Y7xAp/.