Author Topic: Vertically Align an image  (Read 715 times)

0 Members and 1 Guest are viewing this topic.

Offline ryanh_106Topic starter

  • Enthusiast
  • Posts: 50
    • View Profile
Vertically Align an image
« on: January 31, 2007, 09:51:47 PM »
Hi,

I want to display 3 random images on my homepage, each time it loads. They are all no more than 200px high or wide (depending on their orientation), but apart from that I cant be sure how high they are.

I have a container div which is 230x230 and has a background image of like a raised platform onto which I want the image to be placed. I can then center the image horizontally using text-align but i need some ideas on centering vertically. I know its never easy, but im hoping someone has a cheeky workaround i can use.

HTML:

Code: [Select]
<div class="sample">
  <a href="search.php?indexfind=22" title="Click to view property information">
    <img src="includes/image_processor.php?select=22" alt="EXAMPLE PROPERTY" title="Click to view" />
  </a>
</div>

CSS:

Code: [Select]
.sample { width:230px; height:230px; display:block; float:left; background-image:url('../../images/box.jpg'); background-position:center center; background-repeat:no-repeat; text-align:center; }
.sample img { margin-top:15px; }

Thanks

Ryan

Offline AndyB

  • back from the North Pole ... and retired!
  • Staff Alumni
  • Freak!
  • *
  • Posts: 8,521
  • php 5.2.3 / MySQL 5.0.27
    • View Profile
    • halfadot smallwebs
Re: Vertically Align an image
« Reply #1 on: January 31, 2007, 10:56:59 PM »
Workaround? Sure. Use getimagesize() to determine the height of the image that needs to sit on the platform. Do a little math. Position the image the right distance from the top of the container and it'll sit right on top of the platform.
Legend has it that reading the manual never killed anyone.
My site

Offline ryanh_106Topic starter

  • Enthusiast
  • Posts: 50
    • View Profile
Re: Vertically Align an image
« Reply #2 on: February 01, 2007, 10:10:39 AM »
Hi Andy,

Didnt actually use that method in the end (as the images are generated on the fly) but wouldnt have thought of doing it like that without you mentioning it so thanks a lot.

Ended up using javascript after the image had been placed to get its height property then change the style to nudge it down the required amount

Thanks again

Ryan