Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Oct 2008
Location: USA
Posts: 14
#1: Nov 10 '08
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>

Expert
 
Join Date: Aug 2008
Posts: 397
#2: Nov 10 '08

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


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.
Newbie
 
Join Date: Oct 2008
Location: USA
Posts: 14
#3: Nov 10 '08

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


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.
Expert
 
Join Date: Aug 2008
Posts: 397
#4: Nov 10 '08

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


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.
Newbie
 
Join Date: Oct 2008
Location: USA
Posts: 14
#5: Nov 13 '08

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


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.
Reply