Center image within div

This is a discussion on "Center image within div" within the Web Page Design section. This forum, and the thread "Center image within div are both part of the Design Your Website category.



Go Back   Webforumz.com > Main Forums > Design Your Website > Web Page Design

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 14th, 2005, 16:40
Junior Member
Join Date: Aug 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Center image within div

This old chestnut - I've read a bunch of articles on this but still can't get it working.
I have an image withing a fixed size div. I need to keep the div fixed size because I'll be swapping the images using javascript. Also, because the images and their sizes will be changing, I can't center them by fixing the margins, like someone suggested.
So how do I get the picture to be centered vertically & horizontally within the div, and stay centered even when the image (and image size) change?
Here's a snippet:
Code: Select all
<div style="width:500px; height:500px; border:1px solid gray; ">
<img style="text-align:center; vertical-align:middle;" id="mainpic" src="../images/productpics/6681-1099424825.jpg" width=190 height=288>
</div>
Reply With Quote

  #2 (permalink)  
Old Nov 15th, 2005, 05:47
Junior Member
Join Date: Nov 2005
Location: Kennebunk, ME
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Hate_Phones
Re: Center image within div

Have you tried usig %'s in your css for example:
Quote:
#somthing {
margin-top: __%;
margin-bottom: __%;
margin-left: __%;
margin-right: __%;}
If this doesnt work repost and I will try to come up with somthing else.
Reply With Quote
  #3 (permalink)  
Old Nov 17th, 2005, 21:03
herkalees's Avatar
Highly Reputable Member
Join Date: Jul 2005
Location: Massachusetts, USA
Age: 87
Posts: 576
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to herkalees Send a message via MSN to herkalees Send a message via Yahoo to herkalees
Re: Center image within div

in a style sheet, do this:
div img {
vertical-align:middle;
margin:0 auto;
}
Reply With Quote
  #4 (permalink)  
Old Nov 19th, 2005, 17:43
Junior Member
Join Date: Nov 2005
Location: Santa Cruz, CA
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Center image within div

if you are wanting it to be centered completely:

#sample {
text-align:center;
vertical-align:middle;
}
Reply With Quote
  #5 (permalink)  
Old Nov 23rd, 2005, 20:39
herkalees's Avatar
Highly Reputable Member
Join Date: Jul 2005
Location: Massachusetts, USA
Age: 87
Posts: 576
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to herkalees Send a message via MSN to herkalees Send a message via Yahoo to herkalees
Re: Center image within div

Quote:
Originally Posted by Beach Cruise
if you are wanting it to be centered completely:
#sample {
text-align:center;
vertical-align:middle;
}
Sometimes that doesn't work, the only garaunteed way to do it is like this:
(X)HTML
Code: Select all
<div id="box">whatever is in here will be centered both
horizontally and vertically, even while viewer is dragging the
browser window to different dimensions</div>
CSS
Code: Select all
#box {
width: 600px;
height: 400px;
position: relative;
top: 50%;
left: 50%;
margin: -200px 0 0 -300px;
}
This places the top-left corner of the "#box" in the dead center of the screen, then pulls it back by half of it's height, and half of it's width. The end result is the center of the box is in the center of the browser window, no matter how big the browser window is.
Reply With Quote
  #6 (permalink)  
Old Nov 27th, 2005, 22:56
SuperMember

SuperMember
Join Date: Nov 2005
Location: England
Age: 27
Posts: 305
Thanks: 1
Thanked 0 Times in 0 Posts
Re: Center image within div

I'm trying to center two blocks of text and a swf movie, which i've managed to get side by side, inside a <div> container. I found the following tutorial but it hasn't helped:

http://www.w3.org/Style/Examples/007/center.html

Is there something else I must do to get everything within this <div> centred?

I'm using:

margin-left:auto;
margin-right:auto;

but it's just not happening. It's all staying on the left. I know how to get space either side of the swf by using

margin-left:20px;
margin-right:20px;

But I need it all centred!

Thanks.
Reply With Quote
  #7 (permalink)  
Old Nov 28th, 2005, 11:58
Banned Member
Join Date: Sep 2005
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Center image within div

1. put the 3 thangs inside a span & senter the span inside the div.
Reply With Quote
Reply

Tags
center, image, within, 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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i center this? djcritch Web Page Design 6 Feb 25th, 2008 07:06
Position image in the dead center of my webpage Fysicus Web Page Design 7 Sep 26th, 2007 08:38
How to center an image Pádraig Starting Out 8 Jun 12th, 2007 12:27
Center a Swf Help?? nutbolt Web Page Design 20 Apr 9th, 2007 10:21
center align text/background image Lchad Web Page Design 5 Dec 5th, 2006 10:40


All times are GMT. The time now is 18:49.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43