Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > CSS
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 03-24-2006, 07:35 PM
  #1
rockmorg
Novice (Level 1)
 
Join Date: Nov 2005
Posts: 10
iTrader: (0)
rockmorg is an unknown quantity at this point
Vertical-alignment of Images in a DIV

Hey all,

I'm revamping my company's site and doing it in several divs instead of a table layout. So far, things are going quite a bit better than tables. One thing that has been a problem for me is vertical alignment inside my divs. Right now I'm using the padding-top to push the image down and appear to be in the middle, but that will not be good for multiple images.

So, here is the code:

Code:
div.left
{
 padding-top:100px;
 position:relative;
 height:200px;
 float:left;
 width:200px;
 clear:both
}
I have an image in this div that I would like to place in the middle, so I put an inline style on the image:

HTML Code:
<img src="BNCinstall.jpg" alt="BNC installation" style="vertical-align:middle;"/>
And nothing happens... I can use text-align to center the image or move it right or left, but nothing with the vertical-align. Perhaps I need an entirely different method than even vertical-align because on a couple column-like divs I would like to put 3-4 images at different heights.

Anyone have any ideas? Thanks much!
-
Rockmorg
rockmorg is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-24-2006, 10:53 PM
  #2
Kravvitz
Lord (Level 16)
 
Join Date: Jan 2005
Location: USA
Posts: 616
iTrader: (0)
Kravvitz is on a distinguished road
Quote:
This property affects the vertical positioning inside a line box of the boxes generated by an inline-level element. The following values only have meaning with respect to a parent inline-level element, or to a parent block-level element, if that element generates anonymous inline boxes; they have no effect if no such parent exists.

Note. Values of this property have slightly different meanings in the context of tables. Please consult the section on table height algorithms for details.
http://www.w3.org/TR/REC-CSS2/visude...vertical-align

If the only thing in those <div>s is one <img> (not including white-space characters), then setting the line-height to the same as the height in conjunction with vertical-align:middle will work.
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.
Please read this before posting.
Kravvitz is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-25-2006, 11:47 AM
  #3
rockmorg
Novice (Level 1)
 
Join Date: Nov 2005
Posts: 10
iTrader: (0)
rockmorg is an unknown quantity at this point
Hmmm... well I appreciate the feedback and info, but it doesn't seem to address much more than that - so now I know that you pretty much can't use vertical-align for an image in a div unless it is an inline element or there is text or something?

I'm sure this isn't the first time people have wanted images vertically aligned down the center of a div?? There isn't much else out there that rings out vertical alignment to me... anyone else?

Thanks again,
-
Rockmorg
rockmorg is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-25-2006, 12:27 PM
  #4
_Aerospace_Eng_
The CSS Master (somewhat)
 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a galaxy far far away...
Posts: 11,154
iTrader: (0)
_Aerospace_Eng_ has disabled reputation
Kravvitz already gave you a solution. If the image is the ONLY thing in the div then you can set the line height to the height of the div and it will align vertically.
Code:
div.left
{
 padding-top:100px;
 position:relative;
 height:200px;
 float:left;
 width:200px;
 clear:both;
 line-height:200px;
}
Edit:
If you are using more than one image then you can do something like this
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<style type="text/css">
.left {
width:200px;
height:500px;
position:relative;
border:1px solid #000;
margin:auto;
}
.theimgs { /*height of images is 300px and width of images is 100px*/
width:100px;
height:300px;
position:absolute;
top:50%;
left:50%;
margin-top:-150px; /* this is half the height of the images */
margin-left:-50px; /* this is half the width of the images */
background:#F00; /* used for testing purposes because I have no images to use */
}
</style>
</head>

<body>
<div class="left">
	<div class="theimgs"></div>
</div>
</body>
</html>
You will need to know the width and height of the images for the above solution to work.
__________________

76 invites left. PM me for a Gmail invite along with email addy.
Why we won't help you .::. Web Developer's Handbook .::. Why Tables for Layout is Stupid?

Last edited by _Aerospace_Eng_ : 03-25-2006 at 12:33 PM.
_Aerospace_Eng_ is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 03-25-2006, 12:43 PM
  #5
coothead
~ bald headed old fart ~
 
coothead's Avatar
 
Join Date: Aug 2003
Location: chertsey, a small town 25 miles south west of london, england.
Posts: 8,949
iTrader: (0)
coothead has much to be proud ofcoothead has much to be proud ofcoothead has much to be proud ofcoothead has much to be proud ofcoothead has much to be proud ofcoothead has much to be proud ofcoothead has much to be proud ofcoothead has much to be proud ofcoothead has much to be proud ofcoothead has much to be proud of
Hi there rockmorg,

here is a simple example of vertical alignment for images...
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>vertical alignment</title>
<base href="http://coothead.homestead.com/files/">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
<!--
body {
    background-color:#999;
 }
div {
    width:300px;
    height:500px;
    border:1px solid #000;
    background-color:#666;
    margin:auto;
 }

img {
    display:block;
    border:2px solid #000;
    margin:46px auto;
 }
-->
</style>

</head>
<body>

<div>

<img src="anim.gif" alt="">
<img src="anim1.gif" alt="">
<img src="anim2.gif" alt="">

</div>

</body>
</html>
__________________
coothead is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Reply
KEEP TABS
SPONSORS
 
Boxedart

 
 


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



 
  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
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 02:06 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.