Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > Supporting Technologies > HTML and JavaScript

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-07-2009, 06:43 PM   #1
codemaster
Code Master
 
Join Date: Dec 2008
Posts: 21
Question Center <div>

Hello,

I need to be able to center (both horizontally and vertically)
HTML Code:
<div style="position: fixed;">Sample content</div>
and I have found some suggestions elsewhere, but I also want to have it cross-browser compatible.

I've tried
HTML Code:
<div style="position: fixed; width: 100px; margin-left: auto; margin-right: auto;">Sample content</div>
but nothing happens, and if it did, it wouldn't align it vertically.
codemaster is offline   Reply With Quote
Old 11-09-2009, 06:44 PM   #2
jasonJ
Senior Member
 
Join Date: Sep 2006
Location: Poland, EU
Posts: 267
Send a message via ICQ to jasonJ
Default

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
jasonJ is offline   Reply With Quote
Old 11-10-2009, 03:24 AM   #3
codemaster
Code Master
 
Join Date: Dec 2008
Posts: 21
Default

Thanks!
codemaster is offline   Reply With Quote
Old 11-10-2009, 03:30 AM   #4
codemaster
Code Master
 
Join Date: Dec 2008
Posts: 21
Red face

Quote:
Originally Posted by jasonJ View Post
HTML Code:
    <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>
Of course I want it to be
Code:
position: fixed;
which doesn't work in IE...
codemaster is offline   Reply With Quote
Old 11-10-2009, 05:41 PM   #5
jasonJ
Senior Member
 
Join Date: Sep 2006
Location: Poland, EU
Posts: 267
Send a message via ICQ to jasonJ
Default

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..
jasonJ is offline   Reply With Quote
Old 11-10-2009, 10:14 PM   #6
codemaster
Code Master
 
Join Date: Dec 2008
Posts: 21
Default

It works, thanks!
codemaster is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:14 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.