114

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.

Best
  • 2,090
  • 9
  • 30
  • 49
  • related question: https://stackoverflow.com/questions/8639383/how-do-i-center-an-svg-in-a-div Apart from giving the parent (here td) a style of text-align: center, you might have to give the child (here, img) a style of display: block or display: inline-block – mathheadinclouds Jun 24 '20 at 14:54

9 Answers9

190
<td align="center">

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

<td style="text-align: center;">
Scott
  • 20,800
  • 8
  • 42
  • 55
72

Simple way to do it for html5 in css:

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

}

Worked for me perfectly.

Mohammed Gadiwala
  • 1,743
  • 2
  • 17
  • 26
  • It works perfectly; doesn't work, works, but is not supported in HTML5 – Antonio Rizzo Jul 07 '16 at 08:46
  • The "text-align: center" approach seems to work if the image is an inline element. If you use "display: block" to get rid of the extra border pixels around the image, then you need to use this. – fadden Jul 26 '21 at 17:09
11

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>

Not A Bot
  • 2,282
  • 2
  • 15
  • 28
Top Systems
  • 931
  • 12
  • 23
6

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>
Kheteswar
  • 149
  • 2
  • 4
6

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;
}
Michiel
  • 7,571
  • 16
  • 59
  • 111
6
<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

Andrew P.
  • 161
  • 9
oqx
  • 1,920
  • 16
  • 26
4
td image 
{
    display: block;
    margin-left: auto;
    margin-right: auto;
}
aulianza
  • 41
  • 1
0

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

Jeremy Caney
  • 6,191
  • 35
  • 44
  • 70
  • I’ve fixed that for you, but in the future just use backticks (`) on either side of the HTML elements to ensure they are displayed. – Jeremy Caney Jun 12 '20 at 04:32
-6

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"
Jithin Raj P R
  • 6,427
  • 8
  • 36
  • 67
Kheteswar
  • 149
  • 2
  • 4
  • 3
    This suggestion is not advised since it is unsupported in HTML5. Better to use CSS style or class with vertical-align:middle applied to column or container. – mbokil Aug 25 '13 at 22:51