By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,283 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,283 IT Pros & Developers. It's quick & easy.

Centering an two images concentrically in a <TD> or a <DIV>?

P: 14
I am trying to center 2 images concentrically which are z-indexed to lay on top of each other making an image with a border from another image that has a transparent center. I need the images to be horizonally and vertically centered within a <TD> </TD> area. No matter what I do the two image just present one above the other, albeit centered, NOT on top of each other.
I originally did the entire page layout in TABLES until I found out I.E. 7 did not handle the image alignment correctly and I have already rewritten much of the page to get my three <td> areas within my main table to be spaced and centered correctly. Since I have now used <div> tags in many places to correct for formating issues with the nested tables I had, I now have basically two vertical menu bars flanking my imaging space in the center; all this is defined by a table and centered in the viewport. I have tried moving the <DIV> outside the <TD> tags, I have tried no <TD> tags which throws the whole page out of wack, I have tried to put the style attribute in the <TD> declaration and a dozen other things. What the heck is going on with this centering?
See my sample code:

Expand|Select|Wrap|Line Numbers
  1. <Table>
  2. <TR>
  3. <TD style="width:75px;height:400px">
  4. Some stuff.
  5. </TD>
  6. <TD style="width:75px;height:400px">
  7. <div style="width:400px;height:400px; text-align:center; vertical-align:middle;">
  8. <img style="z-index:1;" src="photo1.jpg">
  9. <img style="z-index:2;" src="photo2.jpg">
  10. </div>
  11. </TD>
  12. <TD style="width:75px;height:400px">
  13. Some stuff.
  14. </TD>
  15. </TR>
  16. </Table>
Nov 10 '08 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 397
Clarification needed: what is the width and the height of each image? Better, yet-- put both images on a public server and point to them with a clickable link to each in your post.
Nov 10 '08 #2

P: 14
Here is a link to the test code using the coding in my example above:
http://www.digitalphotoonline.com/greetings/cards/test3.html

The individual images are:
http://www.digitalphotoonline.com/greetings/cards/Card_01-h.gif
http://www.digitalphotoonline.com/upload/z/002.jpg
I can resize the 002.jpg image with php code when the time comes.

My first problem is to get the images concentric.
Is there anyway to use the <div> tag as a "position:relative" block and center of that? I guess I could use "position:absolute" but that just kills the flexibility.
In the future I may have multiple opening "overlays" which may require a different approach.
Nov 10 '08 #3

Expert 100+
P: 397
No tabular data. No table. If this method will work for your situation at hand, no absolute positioning is needed, either. If you want it to render as intended the CSS and markup need to be valid. If you want to cater to the world's most popular browsers -- IE/6 and IE/7 -- the doctype is needed.

Cursory checked in Mac Opera, Safari, Camino, Firefox; and, XP IE/6.0 and IE/7.0.

Please see: some stuff .



HTH.
Nov 10 '08 #4

P: 14
Thank you for your insight and reply David;
I failed to write in my original post that I had thought about and tried using the background image solution, albeit not nearly as elegantly as your solution. However, there will be a need in the future to be able to position more than one image behind a "mask" as some of the designs will require. I have done some experimenting and found that for some reason "vertical-align:middle" does not work with "positioning:relative" if more than one image is in the <DIV>. So what I have done is simply use a PHP function to resize and calculate the height of the rear image and then for the the second image (the overlay mask) use "position:relative;top:-(height of first image+border height)" this negative "top" adjustment puts the overlay mask in the correct location over the first image. The only issue is that the total <DIV> height is adjusted to the sum of the two image heights as if the two images were one above the other i.e., if both images were 400pixels high, the <DIV> height presents itself as 800 pixels even though the two images are now stacked on top of each other. So stylistically it is not perfect, and if I start putting 3 or 4 images into the <DIV> the height of the area could be unreasonably large, throwing off the stlye of the whole page. If this is the case I will have to go the a PHP GD interface solution that I wanted to avoid just now. If you have any thoughts I would welcome them. Thanks again.
Nov 13 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.