| Home | Tutorials | Forums | Articles | Blogs | Movies | Library | Employment | Press | Buy templates |
|
|
#1 |
|
Code Master
Join Date: Dec 2008
Posts: 21
|
Hello,
I need to be able to center (both horizontally and vertically) HTML Code:
<div style="position: fixed;">Sample content</div> I've tried HTML Code:
<div style="position: fixed; width: 100px; margin-left: auto; margin-right: auto;">Sample content</div> |
|
|
|
|
|
#2 |
|
Senior Member
|
The following seem to work both in IE 7 and FF so I guess you can consider it cross-browser ;-)
HTML 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"> #centered-div { background-color: #f8f8f8; border: 1px solid #d6d6d6; padding: 10px; position: absolute; top: 50%; left: 50%; width: 400px; height: 200px; margin-top: -100px; /* set to a negative number 1/2 of your height */ margin-left: -200px; /* set to a negative number 1/2 of your width */ } </style> </head> <body> <div id="centered-div">I am a centered div</div> </body> </html>
__________________
Visit my personal web page |
|
|
|
|
|
|
|
|
#3 |
|
Code Master
Join Date: Dec 2008
Posts: 21
|
Thanks!
|
|
|
|
|
|
#4 | |
|
Code Master
Join Date: Dec 2008
Posts: 21
|
Quote:
Code:
position: fixed; ![]() |
|
|
|
|
|
|
#5 |
|
Senior Member
|
It does work with position:fixed. At least in IE 7 (don't know about IE 6 bacause I haven't got one now).
Perhaps you omitted a DOCTYPE declaration (see the very top of my example). If you omit one, then browsers trigger the quirks mode, and as you can read here, IE supports position:fixed only when in standards mode (that is, with a DOCTYPE declaration). Try again the example, which I modified a bit so that the page is scrollable: HTML 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"> #centered-div { background-color: #f8f8f8; border: 1px solid #d6d6d6; padding: 10px; position: fixed; top: 50%; left: 50%; width: 400px; height: 200px; margin-top: -100px; /* set to a negative number 1/2 of your height */ margin-left: -200px; /* set to a negative number 1/2 of your width */ } p { height:1500px; } </style> </head> <body> <div id="centered-div">I am a centered div</div> <p>A long paragraph</p> </body> </html>
__________________
Visit my personal web page Last edited by jasonJ; 11-10-2009 at 07:48 PM.. |
|
|
|
|
|
#6 |
|
Code Master
Join Date: Dec 2008
Posts: 21
|
It works, thanks!
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|