I have a div with some text inside and absolute position. I can set the left or the right, but is there a way to set the center, so Div’s text would expand in both directions.
So far I could only think about creating exstremly long div and centering text inside.
If your div is positioned absolutely, you can simply set it’s
leftproperty so that it’s centered.Example:
HTML:
CSS:
If you don’t know the width of the inner element, you’ll have to rely on javascript.
Here’s an example using jQuery:
and here’s the fiddle: http://jsfiddle.net/aa93G/ . Play around with the text inside
.inner, and you’ll see that the text stays centered.