I need to create a clickable div with an image (of variable size, but that is smaller than the div) centered both horizontally and vertically within the div.
I made the div clickable with
#image-box a { display: block; height: 100%; width: 100%; }
but can’t seem to center the image vertically.
Try this adjusting width and height of your a element as explained in the comments:
NOTES:
Borders are needed only for visual debug, You can delete them at any time.
The trick here is that you use an absolute positioned div (
#image-box) with a fixed width and height.If you set the
#image-box atop and bottom position to zero the rulemargin:autoputs#image-box aelement in a middle position (on the vertical axis) because it has a fixed height, .If you can or like to solve It using jQuery, try this: