What's the best way to vertical center a div?
+rep![]()

What's the best way to vertical center a div?
+rep![]()
I am learning to code, but could you do in the css. margin-top: 25%; or 50% or something. LOL im probs wrong !
Who me? O'rly:eusa_whis
I've heard people say:
but I've not tryed that yet. I know this works:Code:margin: auto;
CSS obviously.Code:position: absolute; top: 50%; margin top: -[half div's height]px;
When I asked before for alligning divs in a skin I got told to use
v-align: center;
The other day I was in a toilet.
A voice came from the cubicle next to me: "Hello mate, how are you doing?"
I didn't want to be rude, so I said, "Not too bad, thanks."
I heard the voice again. "So, what are you up to?"
Again I answered, "Just having a quick ****... How about yourself?"
Then I heard him say "Sorry, mate, I'll have to call you back. I've got some **** in the cubicle next to me answering everything I say."
Then try the margin: auto; thing. If that doesn't work then you might need javascript to find the height of the element. I'm pretty sure It must be possible without javascript though.
Thats an old html tag and can be done with css. I think you meant valign="middle". When I was looking for information about that I found a possible solution for Jack's problem.
Code:div.blah { vertical-align: middle; }
i swear theres vertical-align: in css
i think they are right.
should work.Code:#div { margin: auto;
but, you can try this:
Code:<div style="background-image: url( '<your-full-image>.png' ); background-position: 50% center; background-repeat: no-repeat; width: <your-images-width>px; height: <your-images-width>px; position: absolute; top: 50%; left: 50%; margin-left: -( <your-images-width> / 2 )px; margin-top: -( <your-images-height> / 2 )px;"></div>
+.net - omg it's coming o_o
Jxhn is right to an extent in using vertical align, but for it to work you must set the parent/containing div to be display: table-cell; here is an example.
Now if you want something centered vertically in the body now that's a different story.Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style type="text/css"> #container { width: 500px; height: 500px; display: table-cell; vertical-align: middle; background-color: #333; } #middle { width: 500px; background-color: #999; } </style> </head> <body> <div id="container"> <div id="middle"> Expands yay :)<br /> <br /> <br /> <br /> and yet still centered :P: </div> </div> </body> </html>
I might also add on Jxhn first post about the position: absolute; left: 50%; margin-left: -(width/2)px of course you don't do it like that you do the calculations this has one slight problem - you've got to set the parent to position: relative; otherwise it'll fail.
Last edited by Iszak; 25-10-2008 at 09:02 PM.
Want to hide these adverts? Register an account for free!