My code is below and I’m looking for the best way via CSS to center an image (horizontally and vertically) in a 300 by 300 pixel square. Larger images will be fit down to that size and smaller images should be centered, not stretched.
<table width="100%">
<tr>
<td><div class="300box"><img class="centeredimage" /></div></td>
<td><div class="300box"><img class="centeredimage" /></div></td>
<td><div class="300box"><img class="centeredimage" /></div></td>
</tr>
</table>
css:
.300box {
height: 300px;
width: 300px;
}
.centeredimage {
vertical-align: middle;
text-align: center;
}
I know the above is incorrect so I’m hoping to find a more efficient way to do it. Each table row has 3 300×300 pixel divs with images centered within.
Using a div inside a td is not worse than any other way of using tables.
You can try this–
CSS:
HTML: