Forum Moderators: not2easy

Message Too Old, No Replies

Horizontal centering issue

         

chuckee

3:55 am on Oct 23, 2007 (gmt 0)

10+ Year Member



Hi,
I cannot seem to figure out how to do horizontal centering even though I have read through countless webpages on the subject. Nothing seems to work, e.g:

<div style="position: absolute; bottom: 8px; width:660px; margin: 0 auto;">
Text to Center! Text to Center! Text to Center!</div>

The code above just leaves the text at the left-hand side of the page.
The code below doesn't center the text properly.

<div align="center">
<div style="position: absolute; bottom: 8px; margin: 0 auto;">
Text to Center! Text to Center! Text to Center!</div></div>

Any ideas why these don't work?
Thanks!

Marshall

5:15 am on Oct 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add to your <div> style which contains the text:

text-align: center;

or, enclose the text in a <p> tag and have the <p> styled:

text-align: center;

If you <div> has an ID, you can do:

#id p {
text-align: center;
}

Marshall

chuckee

5:42 am on Oct 23, 2007 (gmt 0)

10+ Year Member



Thanks but they don't seem to work.
Any more ideas?

Marshall

5:55 am on Oct 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First, what doctype are you using? Second, are you trying to center the <div> the text is in as well as centering the text? If that is the case, your outer <div> should be as shown below and needs a specified width:

<div style="position: absolute; bottom: 8px; margin: 0 auto; width: n">
<div style="text-align: center;">
Text to Center! Text to Center! Text to Center!</div></div>

n = number plus form of calibration: px, %, or em

Marshall

chuckee

7:07 am on Oct 23, 2007 (gmt 0)

10+ Year Member



Thanks, I'm almost there.
I was just wondering why using 100% in place of 'n' produces a horizontal scroll bar?
e.g.

<div style="position: absolute; bottom: 8px; margin: 0 auto; width: 100%">
<div style="text-align: center;">
Text to Center! Text to Center! Text to Center!</div></div>

What value should be used instead of 100% in order to avoid the ugly scrollbar? Is there some rule, or should I just guess at something like 90%?

Marshall

7:32 am on Oct 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The width is the sum total of the assigned width PLUS margins, padding and borders. Right now, I would guess your width really adds up to about 105%. Just keep subtracting off the assign width until it fits. Even then, I would drop it one or two percentages more.

Marshall

chuckee

8:40 am on Oct 23, 2007 (gmt 0)

10+ Year Member



Great thanks very much for your help!