views:

8532

answers:

5

How to center text over an image in a table cell using javascript, css, and/or html?

I have an HTML table containing images - all the same size - and I want to center a text label over each image. The text in the labels may vary in size. Horizontal centering is not difficult, but vertical centering is.

ADDENDUM: i did end up having to use javascript to center the text reliably using a fixed-size div with absolute positioning; i just could not get it to work any other way

+7  A: 

you could try putting the images in the background.

<table>
    <tr>
        <td style="background: url(myImg.jpg) no-repeat; vertical-align: middle; text-align: center">
            Here is my text
        </td>
    </tr>
</table>

You'll just need to set the height and width on the cell and that should be it.

nickf
A: 

I would set the images as the cells' background via CSS, set the cells' size to the proper fixed value (again via CSS), and then insert the text label as the cell content. By default, the content of table cells is centered vertically, so I think you don't have to worry about it. Again, vertical and horizontal alignment can be easily set via CSS. This approach works because I applied it a lot of times.

Another way would be to insert both the image and text in the table cells, wrapping the text in a DIV element and playing with its CSS properties (relative position and margins), but this is a bit tricky in my opinion.

Dario Solera
A: 

You can use TD's option "valign" and it can be top, bottom or center... But as far as I know cell contents are centered vertically by default, so probably your CSS makes them show with bottom or top option.

<TABLE><TR valign=center>
  <TD align=center background="some image"> image label </TD>
</TR></TABLE>
sdkpoly
If you're already using CSS, it would probably be better to set the text-align property for the td, provided it's text or an inline element you're trying to position.
Sam Murray-Sutton
For the love of all things holy, get this man some xhtml!
Oli
Steven asked also for HTML solution so it's HTML. Not everyone has to use xhtml.
sdkpoly
sdkpoly is correct, i don't care whether the solution is html, css, javascript, or whatever - i'm not religious about these sorts of things, i just want it to work consistently
Steven A. Lowe
+1  A: 

There's no proper way of doing it in CSS (although there should be). But here's a method that works for me.

CSS:

#image1, #image1-text, #image1-container {
  overflow: hidden;
  height: 100px;
  width: 100px;
}

#image1 {
  top: -100px;
  position: relative;
  z-index: -1;
}

#image1-text {
  text-align: center;
  vertical-align: middle;
  display: table-cell;
}

HTML:

  <div id="image1-container">
    <img src="image.jpeg" id="image1">
    <div id="image1-text">
      hello
    </div>
  </div>

The order of image1 and image1-text in the container doesn't matter.

It's a bit of a hack but it works anywhere, not just in a table. It doesn't properly work in IE however. It will display it at the top instead. But it works in FF, Safari and Chrome. Haven't tested in IE8.

A hack for IE7 or less, which will only show 1 line, but it will be centred is to add the following inside the <head> tag:

<!--[if lte IE 7]>
<style>
  #image1-text {
    line-height: 100px;
  }
</style>
<![endif]-->
Vincent McNabb
A: 

thanks everyone for the suggestions.

i did end up having to use javascript to center the text reliably using a fixed-size div with absolute positioning; i just could not get it to work any other way.

i also had to generate the text divs with visibility hidden and have a javascript loop at the end of the page to make them visible and place them over the appropriate table cell

there are some serious holes in the layout capabilities of css/html, hopefully these will be addressed in future versions ;-)

Steven A. Lowe