Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1625 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     HTML/XHTML/DHTML/CSS :

The difinitive CSS centering?

Thread title: The difinitive CSS centering?
Closed Thread  
Page 1 of 2 1 2 >
    Thread tools Search this thread Display Modes  
10-04-2007, 03:47 PM
#1
TechnikCoding is offline TechnikCoding
Status: On Vacation
Join date: Sep 2007
Location:
Expertise:
Software:
 
Posts: 15
iTrader: 0 / 0%
 

TechnikCoding is on a distinguished road

  Old  The difinitive CSS centering?

Hey,

I come to you to pose a question that has definatly been asked many a time. What's the best way to center a div in CSS?

Normal instant response:
div {position:relative: top:0; margin:0 auto; width:Npx}

But is this always the best way of doing it? I've seen many other techniques each with thier own drawback. The drawback to this response is its not entirely browser compatible.

Other ways:
div {position:absolute; width:Npx; margin-left:-N/2px; left:50%;}

The second is completly browser safe, but in a browser smaller than N in width, the left gets cut off. Which is best. Due to me being a UK coder, i dont have an iphone yet, but i am sure i will have to re-invent some of my personal coding standards for it. /disucss?

10-04-2007, 04:16 PM
#2
daz is offline daz
Status: I love this place
Join date: Jun 2005
Location: UK
Expertise:
Software:
 
Posts: 562
iTrader: 5 / 100%
 

daz is on a distinguished road

Send a message via MSN to daz

  Old

Code:
margin: 0 auto;

10-04-2007, 04:44 PM
#3
TechnikCoding is offline TechnikCoding
Status: On Vacation
Join date: Sep 2007
Location:
Expertise:
Software:
 
Posts: 15
iTrader: 0 / 0%
 

TechnikCoding is on a distinguished road

  Old

Not much of a contribution as i did say that in my post :P

10-04-2007, 04:55 PM
#4
daz is offline daz
Status: I love this place
Join date: Jun 2005
Location: UK
Expertise:
Software:
 
Posts: 562
iTrader: 5 / 100%
 

daz is on a distinguished road

Send a message via MSN to daz

  Old

margin: 0 auto is 99% cross-browser compatible. I don't think it works in IE 5 or less, but no one uses that anymore.

10-04-2007, 05:24 PM
#5
.com is offline .com
Status: I'm new around here
Join date: Oct 2007
Location:
Expertise: Web design, XHTML/CSS coding
Software:
 
Posts: 16
iTrader: 0 / 0%
 

.com is on a distinguished road

  Old

div { width: xxxpx; margin: 0 auto }
Works in IE, Firefox, Opera, and Safari

10-04-2007, 06:26 PM
#6
Tom is offline Tom
Tom's Avatar
Status: I Code Things
Join date: Aug 2005
Location: UK
Expertise:
Software:
 
Posts: 1,998
iTrader: 14 / 100%
 

Tom is on a distinguished road

Send a message via MSN to Tom

  Old

Originally Posted by .com View Post
div { width: xxxpx; margin: 0 auto }
Works in IE, Firefox, Opera, and Safari
Thats actually right an a correct/update on daz's method. The margin:0 auto; does need a width.

10-04-2007, 07:18 PM
#7
hjalmar is offline hjalmar
Status: Senior Member
Join date: Nov 2004
Location: Sweden
Expertise:
Software:
 
Posts: 857
iTrader: 11 / 100%
 

hjalmar is an unknown quantity at this point

  Old

Adding to the posts above.

That method doesn't work when IE is in Quirksmode. (correct me if im wrong havn't checked it out myself)

10-04-2007, 07:52 PM
#8
patrickPaul is offline patrickPaul
Status:
Join date: Dec 2004
Location: California, US
Expertise:
Software:
 
Posts: 406
iTrader: 1 / 100%
 

patrickPaul is on a distinguished road

Send a message via AIM to patrickPaul Send a message via MSN to patrickPaul Send a message via Yahoo to patrickPaul

  Old

Originally Posted by hjalmar View Post
Adding to the posts above.

That method doesn't work when IE is in Quirksmode. (correct me if im wrong havn't checked it out myself)
Ew... Quirksmode

Best Regards,
Patrick

10-04-2007, 08:16 PM
#9
Amross is offline Amross
Status: Member
Join date: Jan 2007
Location:
Expertise:
Software:
 
Posts: 311
iTrader: 4 / 100%
 

Amross is on a distinguished road

  Old

Code:
body {
     text-align: center;   /* IE Fix */
}

.containerNameHere {
     width: ***px;   /* Width required for margin centering */
     margin: 0 auto;
     text-align: left;   /* Re-align text to the left */
}

10-05-2007, 03:55 PM
#10
TechnikCoding is offline TechnikCoding
Status: On Vacation
Join date: Sep 2007
Location:
Expertise:
Software:
 
Posts: 15
iTrader: 0 / 0%
 

TechnikCoding is on a distinguished road

  Old

Amross's fix is the best method (despite 90% of these responses not reading the actual post haha). The ie fix works. Also remember you have to really set a min-width in the body for completeness.

Closed Thread  
Page 1 of 2 1 2 >


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

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed