tags:

views:

64

answers:

3

I'm now doing it this way,which is not centered:

$div = $('<div style="background-color:yellow;position:absolute;top:0;"><b>Loading...</b></div>').appendTo('body');
+1  A: 

Give it

margin: 0 auto;
adam
Did you run it?Not working ..
Mask
Try adam's answer
Pandiya Chendur
What doctype are you using? It's always worked for me
adam
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Mask
You'll need to remove position: absolute too
adam
No,still not working.This isn't the right way to do it.
Mask
setting left/right margins to auto doesn't work for IE, does it? At least not <=IE7
Andy E
+3  A: 

For auto margins to work you also need to define a width for the div as well, else it just takes up the whole screen width and you'll see no difference.

However, in this instance all you need is to add text-align: center; to your original CSS as far as I can see?

Will Prescott
Do you have firebug by your hand?why not run it to see whether it works?
Mask
...oh, but if you want it absolute positioned you'll need left:0; and right:0; too.
Will Prescott
What does "left:0; and right:0;" do ?
Mask
In this context they make your div full screen width, and text-align:center will put your text in the middle of the div. Full CSS you need would be:background-color:yellow;position:absolute;top:0;left:0;right:0;text-align:center;
Will Prescott
A: 
<div style="text-align: center">
  <div style="text-align: left; margin: 0 auto; width: 50%; background: yellow">
    Your text here
  </div>
</div>
chelmertz