NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Centering with margin: auto

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 12-19-2006, 06:16 PM THREAD STARTER               #1 (permalink)
New Member
Join Date: Nov 2006
Posts: 12
VaporAction is an unknown quantity at this point
 



Centering with margin: auto


I'm new to HTML and CSS...I'm trying to center content on the page so that it will remain centered even when the window is resized...I've tried using margin: auto but that isn't working...
I'm using Firefox, not IE so it's not the browser...

the code runs something like this...
HTML....

<div id="centeredContent">blahblahblah</div>

CSS....

#centeredContent {position:fixed; top: 30px; width: 300px; margin: auto;}
VaporAction is offline  
Old 12-19-2006, 06:24 PM   #2 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
Do you have a doctype set?

If not, use
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 profile="http://gmpg.org/xfn/11">
for the first 3 lines of your code.
Dan is offline  
Old 12-19-2006, 07:12 PM THREAD STARTER               #3 (permalink)
New Member
Join Date: Nov 2006
Posts: 12
VaporAction is an unknown quantity at this point
 



yep...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
VaporAction is offline  
Old 12-19-2006, 08:05 PM   #4 (permalink)
A Wealth of Knowledge
 
stscac's Avatar
Join Date: Aug 2004
Posts: 3,809
stscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud ofstscac has much to be proud of
 



CSS is below
HTML Code:
margin-left: auto; margin-right: auto
-Steve
stscac is offline  
Old 12-19-2006, 09:09 PM   #5 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
margin: auto; sets all sides to margin: auto; and since the top and bottom can't be, it's the same as just doing what you did or margin: 0 auto;

VaporAction, can you show us the page?
Dan is offline  
Old 12-20-2006, 07:17 AM THREAD STARTER               #6 (permalink)
New Member
Join Date: Nov 2006
Posts: 12
VaporAction is an unknown quantity at this point
 



here's the link to the page...

http://www.karlyoder.com/wiredchemist/index.html
VaporAction is offline  
Old 12-20-2006, 08:06 AM   #7 (permalink)
Munky Designs
Join Date: May 2005
Posts: 996
Albino is a jewel in the roughAlbino is a jewel in the roughAlbino is a jewel in the rough
 



im not sure if this will help, but:

body{
text-align:center;
}

#centeredcontent{
margin-left:auto;
margin-right:auto;
}


that will make the content be centered, although it may not be exactly what you want.
Albino is offline  
Old 12-20-2006, 08:42 AM   #8 (permalink)
Gaz
NamePros Member
Join Date: Dec 2006
Location: Plymouth
Posts: 27
Gaz is an unknown quantity at this point
 



Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head><title>Wired Chemist</title>
<style type="text/css">

body {
	padding: 0;
	margin: 0;
} 

#navText {
	text-align: center;
	font-family: Arial, Helvetica, sans-serif;
	color: #4682B4;
	width: 100%;	
	background: #b0c4de;
	margin-top: -30px;
	left: 0;
}

#navH3 {
	font-size: 30px;
	top: 20px;
}
</style>
</head>
<body>
	<div id="navText">
		<h3 id="navH3">Wired Chemist</h3>
		<p id="navP">Chemistry | Mineralogy | Environmental | NMR<br><br>Instructional | Data Tables | Links</p>
	</div>
</body>
</html>
this works better imo.
Gaz is offline  
Old 12-20-2006, 08:49 AM THREAD STARTER               #9 (permalink)
New Member
Join Date: Nov 2006
Posts: 12
VaporAction is an unknown quantity at this point
 



shouldn't margin: auto simply work?
I'm not using IE so I don't need to do the text-align: center...

why the $#@$% isn't margin: auto working????
VaporAction is offline  
Old 12-20-2006, 08:54 AM   #10 (permalink)
Gaz
NamePros Member
Join Date: Dec 2006
Location: Plymouth
Posts: 27
Gaz is an unknown quantity at this point
 



text-align: center; is aligning your text in the centre of the div which is at 100% width. Margin: auto, will assign the left and right margins an equal ammount, but also the top and bottom one, margin: 0px auto; will assign your top and bottom margin's 0 and your left and right ones auto.

Before you can use these techniques, you must first understand how they work.
Gaz is offline  
Old 12-20-2006, 09:33 AM THREAD STARTER               #11 (permalink)
New Member
Join Date: Nov 2006
Posts: 12
VaporAction is an unknown quantity at this point
 



finally figured out the problem...
margin: auto doesn't work with positioning...
which ***** cause I want to have an element whose positioning is fixed vertically (a top navigation bar that will stay fixed as the user scrolls down) but whose width stays centered during resize....anybody know how to do this without resorting to frames?

I tried using width: 100% and text-align: center but multiple lines of text get unaligned with each other if the window is resized...
VaporAction is offline  
Old 12-20-2006, 09:48 AM   #12 (permalink)
Gaz
NamePros Member
Join Date: Dec 2006
Location: Plymouth
Posts: 27
Gaz is an unknown quantity at this point
 



Originally Posted by VaporAction
finally figured out the problem...
????: NamePros.com http://www.namepros.com/programming/271149-centering-with-margin-auto.html
margin: auto doesn't work with positioning...
which ***** cause I want to have an element whose positioning is fixed vertically (a top navigation bar that will stay fixed as the user scrolls down) but whose width stays centered during resize....anybody know how to do this without resorting to frames?

I tried using width: 100% and text-align: center but multiple lines of text get unaligned with each other if the window is resized...
Place all your content in #content
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head><title>Wired Chemist</title>
<style type="text/css">

body {
	padding: 0;
	margin: 0;
} 

#navText {
	text-align: center;
	font-family: Arial, Helvetica, sans-serif;
	color: #4682B4;
	width: 100%;	
	background: #b0c4de;
	margin-top: -30px;
	left: 0;
	position: fixed;
}

#navH3 {
	font-size: 30px;
	top: 20px;
}

#content {
	width: 800px;
	padding-top: 140px;
	margin-left: auto;
	margin-right: auto;
}
</style>
</head>
<body>
	<div id="navText">
		<h3 id="navH3">Wired Chemist</h3>
		<p id="navP">Chemistry | Mineralogy | Environmental | NMR<br><br>Instructional | Data Tables | Links</p>
	</div>
	<div id="content">
	blah
	<br>
	<br>
	<br>
	<br>
	<br><br>
	<br><br><br>
	<br><br><br>blah
	</div>
</body>
</html>
thought u said it doesn't work
Gaz is offline  
Old 12-21-2006, 07:21 AM THREAD STARTER               #13 (permalink)
New Member
Join Date: Nov 2006
Posts: 12
VaporAction is an unknown quantity at this point
 



I said the nav bar doesn't work with margin: auto because it needs to have fixed positioning...the content works fine with margin: auto because it isn't fixed...
VaporAction is offline  
Old 12-21-2006, 04:31 PM   #14 (permalink)
Gaz
NamePros Member
Join Date: Dec 2006
Location: Plymouth
Posts: 27
Gaz is an unknown quantity at this point
 



can you not see i have fixed your problem?
you tried to over complicate something. which is now fixed...
Gaz is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 02:34 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger