124

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,200
  • 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

207
<td align="center">

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

<td style="text-align: center;">
Scott
  • 21,475
  • 8
  • 43
  • 55
81

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,923
  • 2
  • 18
  • 26
  • 1
    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
  • I once delivered a box to a man whose initials were CSS. I asked him if it was okay to leave the box in the center of the room. He kept reiterating that it should be placed where there would be an equal amount of "margin" from the opposing walls. "So, the *center* then?" I confirmed. He gave a blank stare, then explained the placement as "where an object would *automatically* end up if pushed equally from opposing directions". Once again I tried to verify that he meant the center, and again came a long and awkward pause. Yeah, CSS saw things differently from the rest of us... – Mentalist Jun 29 '22 at 08:26
  • This is better, as it affects ONLY the images. Other solutions applied to the td tag also affect texts. – Borjovsky Mar 27 '23 at 00:01
12

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,474
  • 2
  • 16
  • 33
Top Systems
  • 951
  • 12
  • 24
8

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
  • 179
  • 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,855
  • 16
  • 61
  • 113
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
  • 2,157
  • 17
  • 27
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
  • 7,102
  • 69
  • 48
  • 77
  • 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,667
  • 8
  • 38
  • 69
Kheteswar
  • 179
  • 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