Center image in table td in CSS

HtmlCss

Html Problem Overview


I've been trying to align an image to the center of the table td. It worked with setting margin-left to a specific value but it also increased the size of td too and that isn't exactly what I wanted

Is there any efficient ways of doing this? I used percentages for the height and witdh of the table.

Html Solutions


Solution 1 - Html

<td align="center">

or via css, which is the preferred method any more...

<td style="text-align: center;">

Solution 2 - Html

Simple way to do it for html5 in css:

td img{
    display: block;
    margin-left: auto;
    margin-right: auto;

}

Worked for me perfectly.

Solution 3 - Html

Center a div inside td using margin, the trick is to make the div width the same as the image width.

<td>
  <div style="margin: 0 auto; width: 130px">
    <img src="me.jpg" alt="me" style="width: 130px" />
  </div>
</td>

Solution 4 - Html

Set a fixed with of your image in your css and add an auto-margin/padding on the image to...

div.image img {
    width: 100px;
    margin: auto;
}

Or set the text-align to center...

td {
    text-align: center;
}

Solution 5 - Html

<table style="width:100%;">
<tbody ><tr><td align="center">
<img src="axe.JPG" />
</td>
</tr>
</tbody>
</table>

or

td
{
    text-align:center;
}

in the CSS file

Solution 6 - Html

This fixed issues for me:

<style>
.super-centered {
	position:absolute; 
	width:100%;
	height:100%;
	text-align:center; 
    vertical-align:middle;
	z-index: 9999;
}
</style>

<table class="super-centered"><tr><td style="width:100%;height:100%;" align="center"     valign="middle" > 
<img alt="Loading ..." src="/ALHTheme/themes/html/ALHTheme/images/loading.gif">
</td></tr></table>

Solution 7 - Html

td image 
{
    display: block;
    margin-left: auto;
    margin-right: auto;
}

Solution 8 - Html

Another option is the use <th> instead of <td>. <th> defaults to center; <td> defaults to left.

Solution 9 - Html

As per my analysis and search on the internet also, I could not found a way to centre the image vertically centred using <div> it was possible only using <table> because table provides the following property:

valign="middle"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBestView Question on Stackoverflow
Solution 1 - HtmlScottView Answer on Stackoverflow
Solution 2 - HtmlMohammed GadiwalaView Answer on Stackoverflow
Solution 3 - HtmlTop SystemsView Answer on Stackoverflow
Solution 4 - HtmlMichielView Answer on Stackoverflow
Solution 5 - HtmloqxView Answer on Stackoverflow
Solution 6 - HtmlKheteswarView Answer on Stackoverflow
Solution 7 - HtmlaulianzaView Answer on Stackoverflow
Solution 8 - Htmluser13731874View Answer on Stackoverflow
Solution 9 - HtmlKheteswarView Answer on Stackoverflow