I have the following code that produces a background image in a table cell. I would like to put that image in the center of the cell.
<tr>
<td valign="top" width=<%=width%>>
<div id="loading"
style="background-image: url(spinner.gif); height: 16px; width: 16px;">
</div>
</td>
</tr>
I tried align=center but that didn’t work and neither did text-align: center.
Centering your background image
You’re looking for the
background-positionproperty.You could also coalesce the properties into the shorter
backgroundproperty:Centering your element within your cell
One other reading of your question is that you want to center your loading animation in your table cell. If that’s the case, I would encourage you to use the CSS properties
text-alignandvertical-align.With the following HTML
You can see a working demo online at http://jsbin.com/udehox/
it’s worth noting that your
DIVis a block element, and doesn’t displayinlineby default. As such, you won’t be able to “center” it, since it doesn’t permit any space on its left or right.You can force it to display as an inline element by attaching the
display: inlineordisplay: inline-blockstatements to it.