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

Latest Blog Post:
A Bee and a White Wagtail
Posts: 14
Location: Sweden
Hi!

I would like to center align a div.

Like this:



Could someone please help me!
__________________
Thor

Thor's Photoblog
Thort is offline
Reply With Quote
View Public Profile Visit Thort's homepage!
 
When You Register, These Ads Go Away!
Old 04-15-2007, 12:20 PM Re: How to center align a div?
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
<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).
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 04-15-2007, 12:46 PM Re: How to center align a div?
Thort's Avatar
Novice Talker

Latest Blog Post:
A Bee and a White Wagtail
Posts: 14
Location: Sweden
Thanks Adam!

Very Good! It works!

I didn't know about margin: auto
__________________
Thor

Thor's Photoblog
Thort is offline
Reply With Quote
View Public Profile Visit Thort's homepage!
 
Old 04-15-2007, 02:50 PM Re: How to center align a div?
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Not many people do. But any time I can pass that bit of mad knowledge onto someone, I'll do it.
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 04-20-2007, 04:18 AM Re: How to center align a div?
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,024
Name: Forrest Croce
Location: Seattle, WA
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. )
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 04-20-2007, 04:24 PM Re: How to center align a div?
LadynRed's Avatar
Super Moderator

Posts: 6,718
Location: Tennessee
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 !!
"Using or working with IE is like having to wear a 1970's polyester suit with pantyhose and a girdle, to work everyday"
Carolina Corvette Club
LadynRed is offline
Reply With Quote
View Public Profile
 
Old 04-20-2007, 06:46 PM Re: How to center align a div?
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
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.
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 04-21-2007, 05:51 AM Re: How to center align a div?
Thort's Avatar
Novice Talker

Latest Blog Post:
A Bee and a White Wagtail
Posts: 14
Location: Sweden
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

Thor's Photoblog
Thort is offline
Reply With Quote
View Public Profile Visit Thort's homepage!
 
Old 04-21-2007, 12:40 PM Re: How to center align a div?
LadynRed's Avatar
Super Moderator

Posts: 6,718
Location: Tennessee
That's because you're telling it to center everything INSIDE that div.
__________________
Web Goddess & Web Standards Evangelist :) - Tables Be Gone !!
"Using or working with IE is like having to wear a 1970's polyester suit with pantyhose and a girdle, to work everyday"
Carolina Corvette Club
LadynRed is offline
Reply With Quote
View Public Profile
 
Old 04-22-2007, 12:47 PM Re: How to center align a div?
Extreme Talker

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

Or in short hand as Adam said:
margin: 0px auto;
__________________
Work In Progress: http://vagp.eu/index2.html
twiggy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to center align a div?
 

Thread Tools

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


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


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

 


Page generated in 0.15709 seconds with 12 queries