Forum Moderators: not2easy
It's easier to explain my question showing what I want to achieve and what I have tried: [snip]
Hope you can help.
Joakim
[edited by: heini at 5:40 pm (utc) on July 11, 2003]
[edit reason] sorry - please don't use urls [/edit]
Please review the Terms of Service [webmasterworld.com] and what it says about posting URLs.
Now, the only thing that really works (cross-browser) is playing with absolute positioning and negative margins. To acheive the effect you need to know the object's height.
#myobject {
height: 300px;
left: 0;
position: absolute;
top: 50%;
margin-top: -150px;
}
The negative margin (in bold) is the key.
Enjoy! :)
Posting links to personal sites is against the TOS [webmasterworld.com] here at WebmasterWorld. You should really edit your post to remove it before a mod/admin does!
To center text, use text-align:center. The vertical-align was discussed here [webmasterworld.com]. Although there are browser support difficulties.
<added>DrDoc's negative margin example is the currently accepted best workaround!</added>
Back to my question:
What I want to do is not to vertically align an element. We can take two lines of text inside a div as an example: The first line is half the width of the second line, but we don't know the width of any of those lines. Then we left-align this text using 'text-align' in the div. Finally I want to move this 'block' of text into the center of the screen, but the lines position relative to each other shouldn't change, so then I can't set the 'text-align' property to 'center', because that would make so that the first letter of the second line isn't positioned just below the first letter of the first line.
This behaviour WOULD work if I explicitly set a width for the div, and the set 'margin-left' and 'margin-right' to 'auto', but how do I do this without explicitly setting any width/padding/margin? (auto values are ok)
Jocke
Or is it some sort of "typical" centering that you want? If it's this that you want, then perhaps you could achieve approximate results by setting the left margin of your div to something suitable.