TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-25-2008, 02:39 PM   #1 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default Centering DIV to resolution

Hey guys, I need to fix this problem ASAP. It's for a client.

I have a design that is about 800pixels. Im trying to center it to all resolutions in css. My "wrapper" is in the body:

<body id="wrapper">

and basically its suppose to wrap the entire body into a position where it will look centered, but its not. Here is the code:

Code:
#wrapper {
	position: relative;
	margin: 0 auto;
	padding: 0;
ive tried adding 800px as the width height and everything. I'm going crazy
9three is offline  
Reply With Quote
Old 10-25-2008, 03:01 PM   #2 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

First, make a div like: <div id="wrapper">.

Then apply the following css:

Code:
body { text-align: center; }
#wrapper { margin: 0 auto; }
This would center the div crossbrowser.
maZtah is offline  
Reply With Quote
Old 10-25-2008, 03:07 PM   #3 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

im looking for the entire DIV to be centered not just the text. The text where its at, its suppose to be there.
9three is offline  
Reply With Quote
Old 10-25-2008, 03:35 PM   #4 (permalink)
FSX
The Wanderer
 
FSX's Avatar
 
Join Date: Oct 2008
Posts: 15
Thanks: 0
FSX is on a distinguished road
Default

Code:
#wrapper { margin: 0 auto;width: 80% }
A width must also be defined if you would like the center the div. If you don't set one the DIV get a width of 100% like every block element would do.

The body { text-align: center } is only necessary if you want this to work in IE6.
__________________
61924
FSX is offline  
Reply With Quote
Old 10-25-2008, 06:01 PM   #5 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

Quote:
Originally Posted by 9three View Post
im looking for the entire DIV to be centered not just the text. The text where its at, its suppose to be there.
Like FSX said, it's a fix to get it to work in IE6.

And indeed, you have to set a width on the wrapper.
maZtah is offline  
Reply With Quote
Old 10-25-2008, 06:55 PM   #6 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

#wrapper { margin: 0 auto; width:800px; }

will work in all browsers. You dont need a text align center in the body tag. I have never done that and it always works in all browsers.
CoryMathews is offline  
Reply With Quote
The Following User Says Thank You to CoryMathews For This Useful Post:
9three (10-26-2008)
Old 10-27-2008, 03:15 PM   #7 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

cory it worked but what I'm noticing is that its not wrapping around the other DIVs. Is it because i made them absolute?

thanks!
9three is offline  
Reply With Quote
Old 10-27-2008, 09:23 PM   #8 (permalink)
FSX
The Wanderer
 
FSX's Avatar
 
Join Date: Oct 2008
Posts: 15
Thanks: 0
FSX is on a distinguished road
Default

If want #wrapper to wrap around absolute div you got to add position: relative;
to #wrapper.
__________________
61924
FSX is offline  
Reply With Quote
Old 10-28-2008, 01:57 AM   #9 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

hm i did and it didnt do anything :/
9three is offline  
Reply With Quote
Old 10-28-2008, 02:22 AM   #10 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

Ye the absolute will position that tag in the exact location you place it no matter what. In general you should use relative, , which I believe is the default if you don't set one, and try to avoid absolute. However I say thay and sometimes it is much easier to use absolute positioning, but I wouldn't use it on a lot of different elements, It will lead to future problems. Try removing all of the absolute positioning and then going from there and see what happens.
CoryMathews is offline  
Reply With Quote
Old 11-09-2008, 03:19 PM   #11 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

If you all still are wondering how to do this, this is the most logic, easy and compatible way to all resolutions, browsers and OS's.

HTML code
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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />

<title>Untitled Document</title>
</head>

<body>

    <div class="masterWrapper">
        
        <div class="masterContainer">
            
        </div>
    
    </div>

</body>
</html>
CSS code
Code:
@charset "utf-8";
    
    * {
    
        width: 100%;
        height: 100%;
        margin: 0px;
        padding: 0px;
        
    }

    body {
        
        width: 100%;
        height: 100%;
        margin: 0px;
        padding: 0px;
        font-size: 11px;
        font-family: Verdana, Geneva, sans-serif;
        color: #000000;
        
    }
    
        .masterWrapper {
        border: 1px red dashed;
            display: block;
            position: absolute;
            width: 100%;
            height: 100%;
            
        }
        
            .masterWrapper .masterContainer {
            border: 1px red dashed;
                display: block;
                position: absolute;
                left: 50%;
                top: 50%;
                width: 250px;
                height: 250px;
                margin-left: -125px; /* /2 width */
                margin-top: -125px; /* /2 height */
                
            }
position: fixed; works, position: relative; works as well, but the best to use is absolute
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 11-09-2008, 06:21 PM   #12 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

You really want to avoid the

* { width: 100%; height: 100%; margin: 0px; padding: 0px; }

Thats terrible programming. It says you want to apply that to every single tag on the page. Why would you want every tag to be like that?

The easiest way to do this is simply this

HTML

Quote:
...
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="box">
content here.
</div>
</body>
</html>

CSS

Quote:
#box{ background:#333; margin:15px auto; width:960px; }
CoryMathews is offline  
Reply With Quote
Old 11-09-2008, 06:55 PM   #13 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

I admit that the width and so forth are totally not necessary and could completely mess up the page. As for the font-size, color and of course padding and margin, that's just general knowledge. You don't want auto-padding, you want to correct and adjust it manually.

The charset is simply the type of encoding used.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply



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

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:23 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design