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
Centering images in divs
Old 11-19-2008, 12:20 PM Centering images in divs
shonkyboy's Avatar
Extreme Talker

Posts: 226
Name: Chris
Location: West Yorks , UK
Trades: 0
Hi - sorry for asking this as im sure its been asked before - cant find it thogh.
Centering an inage in a div - the old way was <div align=center>
what is the best way to do it with css? I think i tried creating a class with text align=center and it doesnt seem to work.
What is best practice for this?

Many Thanks
__________________

Please login or register to view this content. Registration is FREE
shonkyboy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-19-2008, 12:35 PM Re: Centering images in divs
aldor's Avatar
Ultra Talker

Posts: 479
Name: Alan
Location: Lincoln(UK)
Trades: 0
Try this link:-

http://www.solstice.co.il/2008-02-26...c_width_in_css
aldor is offline
Reply With Quote
View Public Profile
 
Old 11-19-2008, 12:42 PM Re: Centering images in divs
Mark Henderson's Avatar
Extreme Talker

Posts: 207
Name: Mark Henderson
Location: Yorkshire
Trades: 0
make the div class have
text-align:center;

and the image have
margin: auto;
__________________

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


Please login or register to view this content. Registration is FREE
- Affordable Web/Graphics Design -
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
ify - WHOIS Lookup, reverse IP, traceroute and more!
Mark Henderson is offline
Reply With Quote
View Public Profile Visit Mark Henderson's homepage!
 
Old 11-19-2008, 01:32 PM Re: Centering images in divs
Skilled Talker

Posts: 88
Trades: 0
<div style="text-align: center">img code here</div>

will do it for you
__________________
Graham Barnes


Please login or register to view this content. Registration is FREE
xpguy is offline
Reply With Quote
View Public Profile
 
Old 11-19-2008, 02:41 PM Re: Centering images in divs
LadynRed's Avatar
Defies a Status

Posts: 10,017
Location: Tennessee
Trades: 0
Nope, here's an easy and (better) method.

Create a css class for your image, like this:

.imgCenter{
display:block;
margin: 0 auto; (adjust top and bottom margins as needed)
}

That's it. Then in your html, for that image just use;
<img class="imgCenter" src="blahblahblah" />

Image centered in any 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 11-19-2008, 02:57 PM Re: Centering images in divs
Skilled Talker

Posts: 88
Trades: 0
well the truth is no one is wrong as they all work, however some have pros and cons there are easy ways and there are more difficult ways such as you can do this:

.content img {
margin: 0px auto;
display: block
}

<div class="content">content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here content here

content here content here content here content here content here <img src="blahblahblah" />content here</div>

this way means you won't have to add class attributes to every image but also means every image in the content class will be centered and doesn't put the new image on a new line.
__________________
Graham Barnes


Please login or register to view this content. Registration is FREE
xpguy is offline
Reply With Quote
View Public Profile
 
Old 11-19-2008, 03:01 PM Re: Centering images in divs
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
That's normally what I do also (what LadynRed pointed out), but if you ever have an image inside of an anchor (<a href="wherever.html"><img src="whatever.jpg" /></a>), having the image set to "block" will unfortunately make the hover space of the link (that makes the cursor into a hand) take up all of the available horizonal space, which is confusing. Therefore, knowing that images are inline elements, and can also be centered with text-align: center, is good knowledge to have.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-19-2008, 03:01 PM Re: Centering images in divs
LadynRed's Avatar
Defies a Status

Posts: 10,017
Location: Tennessee
Trades: 0
That would definitely work too, provided you want ALL the images inside .content to be centered. So, take your pick
__________________
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 11-19-2008, 03:36 PM Re: Centering images in divs
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Of course, you could put a single div wrapper around the one image, which is slightly redundant, but it works. Most cases I would just block it and auto margin it, like you pointed out.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is online now
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-25-2008, 11:31 AM Re: Centering images in divs
shonkyboy's Avatar
Extreme Talker

Posts: 226
Name: Chris
Location: West Yorks , UK
Trades: 0
thanks for that guys
__________________

Please login or register to view this content. Registration is FREE
shonkyboy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Centering images in divs
 

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.46737 seconds with 12 queries