Connecting Tech Pros Worldwide Help | Site Map

Absolute Position Fix

Familiar Sight
 
Join Date: Apr 2006
Posts: 133
#1: Aug 11 '08
Im trying to get an image to center in a div. It works as long as one part of my CSS is commented out. The issues is the part that is breaking it, is required for another script to run that I have not added in due to its vast amount of code. Can anyone tell me a workaround.. leaving the MUST have code in place. Im willing to add anything to the code, just not remove if possible.

Full Code
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4.  
  5. <style type="text/css">
  6. #image {
  7.     display: table-cell;
  8.     text-align: center;
  9.     vertical-align: middle;
  10.     width:356px;
  11.     height:356px;
  12.     border:1px solid blue;
  13. }
  14. #image * {
  15.     vertical-align: middle;
  16. }
  17. /*\*//*/
  18. #image {
  19.     display: block;
  20. }
  21. #image span {
  22.     display: inline-block;
  23.     height: 100%;
  24.     width: 1px;
  25. }
  26. /**/
  27. </style>
  28. <!--[if IE]><style>
  29. #image span {
  30.     display: inline-block;
  31.     height: 100%;
  32. }
  33. </style><![endif]-->
  34.  
  35. <style type="text/css">
  36. #image img {position:absolute;}
  37. </style>
  38.  
  39. </head>
  40.  
  41. <body>
  42.  
  43. <div id="image"><span></span><img src="http://www.google.com/logos/olympics08_rhythm.gif"></div>
  44.  
  45. </body>
  46. </html>
  47.  
Code that MUST stay in the CSS
Expand|Select|Wrap|Line Numbers
  1. #image img {position:absolute;}
Just to note, the rest of the code is for the most part an exact dup of the cross-browser image center in div code here, http://www.brunildo.org/test/img_center.html
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,561
#2: Aug 11 '08

re: Absolute Position Fix


Just slapped this together. You'll need to adjust it to fit it into your scheme.
[HTML]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title></title>
<style type="text/css">
img{position:absolute;left:50%;margin-left:-101px}
#image{position:relative;outline:1px solid red}
</style>
</head>
<body>
<div id="image"><span></span><img src="1.gif"></div>
</body>
</html>
[/HTML]

The 'margin-left' is one-half of the width of the image.
Reply


Similar HTML / CSS bytes