Mask
Mask

Reputation: 34197

How to make a div top center?

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');

Upvotes: 0

Views: 5645

Answers (3)

chelmertz
chelmertz

Reputation: 20601

<div style="text-align: center">
  <div style="text-align: left; margin: 0 auto; width: 50%; background: yellow">
    Your text here
  </div>
</div>

Upvotes: 0

Will Prescott
Will Prescott

Reputation: 4053

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?

Upvotes: 3

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

Give it

margin: 0 auto;

Upvotes: 1

Related Questions