Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-06-2012, 01:15 AM   PM User | #1
designer21
New Coder

 
Join Date: Nov 2011
Location: England
Posts: 62
Thanks: 10
Thanked 3 Times in 3 Posts
designer21 is an unknown quantity at this point
Div Postitioning

Hi

I am currently building a website as part of my FinalMajor project for college.I have laid out all the divs where i want them all to be however i would like to position them all in the center of the browser instead of the left where they are now.

Is there a simple piece of code i can use to do this?

I am using a an external CSS sheets as well if that helps.

Any help is greatly appreciated

Adam

Last edited by designer21; 04-06-2012 at 09:13 PM..
designer21 is offline   Reply With Quote
Old 04-06-2012, 02:05 AM   PM User | #2
Excavator
Master Coder


 
Excavator's Avatar
 
Join Date: Dec 2006
Location: Alaska
Posts: 9,410
Thanks: 22
Thanked 1,765 Times in 1,749 Posts
Excavator has a spectacular aura aboutExcavator has a spectacular aura aboutExcavator has a spectacular aura about
Hello designer21,
To center an element you need three things:
  1. a valid DocType
  2. an element with a width
  3. that elements right/left margins set to auto


Typically, for an entire site, you would center a containing element and place your divs inside that.
__________________
Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad
Excavator is offline   Reply With Quote
Old 04-06-2012, 02:26 AM   PM User | #3
Myskillzownu
New Coder

 
Join Date: Mar 2012
Posts: 30
Thanks: 6
Thanked 1 Time in 1 Post
Myskillzownu is an unknown quantity at this point
What Excavator said is exactly right.

Just follow steps one and two and for step three just type
margin-left: auto;
margin-right: auto; in your css
Myskillzownu is offline   Reply With Quote
Users who have thanked Myskillzownu for this post:
designer21 (04-06-2012)
Old 04-06-2012, 02:33 AM   PM User | #4
Mishu
Banned

 
Join Date: Mar 2012
Posts: 306
Thanks: 1
Thanked 28 Times in 28 Posts
Mishu can only hope to improve
Quote:
Originally Posted by designer21 View Post

Is there a simple piece of code i can use to do this?
Code:
        
        <style type="text/css">
            #wrapper{
                width: 900px;
                margin: 10px auto 0px auto;
                border : 1px solid blue;
            }
        </style>
Code:
    
    <body>
        <div id="wrapper">
            Page contents go here
        </div>
    </body>
Mishu is offline   Reply With Quote
Old 04-06-2012, 03:26 AM   PM User | #5
Excavator
Master Coder


 
Excavator's Avatar
 
Join Date: Dec 2006
Location: Alaska
Posts: 9,410
Thanks: 22
Thanked 1,765 Times in 1,749 Posts
Excavator has a spectacular aura aboutExcavator has a spectacular aura aboutExcavator has a spectacular aura about
I always like to supply valid code when providing examples to a newbie that may have no idea about DocTypes or how an .html document is laid out. We can always address linked/attached CSS later if needed.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html, body {
	margin: 0;
	background: #fc6;
}
#container {
	height: 600px; /*for demo only*/
	width: 800px;
	margin: 30px auto;
	background: #999;
}
</style>
</head>
<body>
    <div id="container">
Your very interesting website would go here
    <!--end container--></div>
</body>
</html>
__________________
Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad
Excavator is offline   Reply With Quote
Users who have thanked Excavator for this post:
designer21 (04-06-2012)
Old 04-06-2012, 03:35 AM   PM User | #6
Mishu
Banned

 
Join Date: Mar 2012
Posts: 306
Thanks: 1
Thanked 28 Times in 28 Posts
Mishu can only hope to improve
Quote:
Originally Posted by Excavator View Post
We can always address linked/attached CSS later if needed.
probably not needed as the op said they are currently using an external stylesheet so the contents in style blocks can go in the external css.
Mishu is offline   Reply With Quote
Old 04-06-2012, 12:52 PM   PM User | #7
designer21
New Coder

 
Join Date: Nov 2011
Location: England
Posts: 62
Thanks: 10
Thanked 3 Times in 3 Posts
designer21 is an unknown quantity at this point
Thanks for your help guys, for some reason when i tried putting a wrapper div it didn't center it with thhe margins set to auto howeer when i did the divs seperately they did, thanks again
designer21 is offline   Reply With Quote
Reply

Bookmarks

Tags
css, div, html, position

Jump To Top of Thread


Thread Tools
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 +1. The time now is 10:20 PM.


Advertisement
Log in to turn off these ads.