Connecting Tech Pros Worldwide Forums | Help | Site Map

Alternative to the <center> tag

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#1: Oct 2 '08
To accomplish what I am looking for (the image centered in it's parent top 'centered') I have done this:
Expand|Select|Wrap|Line Numbers
  1. <div id="backing">
  2.    <div id="lbImgBacker">
  3.       <center>
  4.          <img id="Image1" src="banner.JPG" alt="" style="border-width:0px;" />
  5.       </center>
  6.    </div>
  7.    <div id="ExampleLeftAlignElement" ></div> 
  8.    <!-- other elements and the rest of the page -->
  9. </div>
  10.  
The following CSS is also applied
Expand|Select|Wrap|Line Numbers
  1. #backing
  2. {
  3.     z-index: 100;
  4.     position: static;
  5.     display: block;
  6.     width: 760px;
  7.     background-color: #faebd8;
  8.     text-align:left; 
  9. }
  10. #Image1
  11. {
  12.     z-index: 100;
  13.     display: block;
  14.     height: 127px;
  15.     width: 756px;
  16.     border-width: 0px;
  17.     cursor: pointer ;
  18. }
  19.  
  20. #lbImgBacker
  21. {
  22.     z-index: 100;
  23.     position: static;
  24.     display: inline;
  25.     text-align: center;
  26.     width: 760px;
  27.     height: 130px;
  28.     color: #804000;
  29.     background-color: Transparent;
  30.     font-weight: bold;
  31.     font-size: 12pt;
  32. }
  33.  

The "text-align:left" in #backing is required for the rest of the page, and seems to override any child styles I attemp to apply.

For arguments sake, "lbImgBacker" is the same width as "backing" and doesn't matter if the text-align is left/center/right. But the image INSIDE "lbImgBacker" should be centered inside of it. This is what I cannot seem to do with style properties. That <center> tag is the only thing that does it, but is not allowed by W3 to be there.
Do I have an alternataive?

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

re: Alternative to the <center> tag


Delete the opening and closing center tags. Add this to the CSS:
Expand|Select|Wrap|Line Numbers
  1. #backing
  2. { margin: 0 auto;}
  3. #Image1
  4. { margin: 0 auto;}
  5.  
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#3: Oct 2 '08

re: Alternative to the <center> tag


Well I don't understand it, but that seemed to do the trick. Thanks!
Expert
 
Join Date: Aug 2008
Posts: 397
#4: Oct 3 '08

re: Alternative to the <center> tag


Quote:

Originally Posted by Plater

Well I don't understand it, but that seemed to do the trick. Thanks!

I share your opinion. Or, do I?
w3c
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#5: Oct 3 '08

re: Alternative to the <center> tag


Quote:

Originally Posted by Plater

Well I don't understand it, but that seemed to do the trick. Thanks!

If you set the margin to auto, the browser will (should) try to automatically calculate the margin on all sides of the element, effectively placing it at the center.

This usually depends on both the inner and outer elements to have a fixed width so the margin can be calculated. This doesn't appear to work with vertical alignment tho, which is kind of weird considering that it's basically the same calculations.
Expert
 
Join Date: Aug 2008
Posts: 397
#6: Oct 3 '08

re: Alternative to the <center> tag


Quote:

Originally Posted by Atli

This doesn't appear to work with vertical alignment tho, which is kind of weird considering that it's basically the same calculations.

Affirmative, Major Tom. Take your protein pill. Put your helmet on.

Ground Control,
Huston
Reply