Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

CSS Forum


You are currently viewing our CSS Forum as a guest. Please register to participate.
Login



Reply
How to center align a div?
Old 04-15-2007, 10:36 AM How to center align a div?
Thort's Avatar
Average Talker

Latest Blog Post:
Popular Foxglove
Posts: 19
Location: Sweden
Trades: 0
Hi!

I would like to center align a div.

Like this:



Could someone please help me!
__________________
Thor


Please login or register to view this content. Registration is FREE
Thort is offline
Reply With Quote
View Public Profile Visit Thort's homepage!
 
 
Register now for full access!
Old 04-15-2007, 11:20 AM Re: How to center align a div?
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,935
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
<div style="margin: 0px auto; text-align: center; width: 760px;"> (replace width as appropriate)
<div style="text-align: left;"> (counters the centering above, which is only for old versions of IE anyway).
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 04-15-2007, 11:46 AM Re: How to center align a div?
Thort's Avatar
Average Talker

Latest Blog Post:
Popular Foxglove
Posts: 19
Location: Sweden
Trades: 0
Thanks Adam!

Very Good! It works!

I didn't know about margin: auto
__________________
Thor


Please login or register to view this content. Registration is FREE
Thort is offline
Reply With Quote
View Public Profile Visit Thort's homepage!
 
Old 04-15-2007, 01:50 PM Re: How to center align a div?
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,935
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
Not many people do. But any time I can pass that bit of mad knowledge onto someone, I'll do it.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 04-20-2007, 03:18 AM Re: How to center align a div?
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
So on that note, care to take a look into why that's not working on a page I'm trying to build? I'm using this code:

<img src="images/Nally.jpg" alt="Nally in black and white" style="margin:0 auto; text-align:center" />

to try to center an image at the bottom of the first page in a new site I'm building: http://seattle-portraits.com/

I know this is a pretty amateurish question ... I'm just not sure why what I'm doing isn't working.

( PS: The site runs through an iFrame - I'm working on fixing that - but I don't want to post the "real" address here because I don't want it to come up as duplicate content. )
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 04-20-2007, 03:24 PM Re: How to center align a div?
LadynRed's Avatar
Defies a Status

Posts: 10,017
Location: Tennessee
Trades: 0
Put the proper width and height measurements on the image. You should ALWAYS put in the width and height, otherwise you're forcing the browser to figure it out and you don't want that.
__________________
Web Goddess & Web Standards Evangelist :) - Tables Be Gone !!

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

LadynRed is offline
Reply With Quote
View Public Profile
 
Old 04-20-2007, 05:46 PM Re: How to center align a div?
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,935
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
The major measurement is the width in this case, though, Forrest. While LNR is correct about specifying both, in your CSS you must specify the width in order to ensure that it knows how wide the element is supposed to be.

The other thing I'm not sure about (mostly because I've never tried it) is whether the centering method will work on an image since it's an inline element. Mind you, that one you'll have to play with. If the image doesn't work, a div definitely will.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 04-21-2007, 04:51 AM Re: How to center align a div?
Thort's Avatar
Average Talker

Latest Blog Post:
Popular Foxglove
Posts: 19
Location: Sweden
Trades: 0
This works fine for centering an image on my blog:

<div style="text-align: center;">

<img src="http://thort.se/pictures/jpg/hen_in_stairs.jpg" width="367" height="543" alt="" />

</div>
__________________
Thor


Please login or register to view this content. Registration is FREE
Thort is offline
Reply With Quote
View Public Profile Visit Thort's homepage!
 
Old 04-21-2007, 11:40 AM Re: How to center align a div?
LadynRed's Avatar
Defies a Status

Posts: 10,017
Location: Tennessee
Trades: 0
That's because you're telling it to center everything INSIDE that div.
__________________
Web Goddess & Web Standards Evangelist :) - Tables Be Gone !!

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

LadynRed is offline
Reply With Quote
View Public Profile
 
Old 04-22-2007, 11:47 AM Re: How to center align a div?
Extreme Talker

Posts: 188
Trades: 0
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;

Or in short hand as Adam said:
margin: 0px auto;
__________________

Please login or register to view this content. Registration is FREE
twiggy is offline
Reply With Quote
View Public Profile Visit twiggy's homepage!
 
Reply     « Reply to How to center align a div?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.74393 seconds with 12 queries