Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

image alignment

Featured Replies

Hi

 

I'm building an image gallery. My images are all different sizes and I have them centralised using text-align for horizontal and setting the margin-top value for vertical. Each time a new image is shown, I look in an array for the corresponding margin value. It all works fine except in IE, where the old image always takes the new margin value before being replaced by the new image. This makes it appear to move briefly before disappearing.

 

I've tried setting the containing div display to 'none' while the images swop over, and tried all orders of changing the settings, but I can't get the old image to disappear with its old margin, then the new one to appear with it's new margin. In Firefox the code below works fine:

 

get("main_image").style.display="none"; //hide the old image

get("main_image").src=''; //remove the old image

get("main_image").src=portfolioDir + array_names[imagePos] + '.jpg'; //get and set the new image

get("main_image").style.paddingTop=array_m_tops[imagePos] + 'px'; //give new image its top padding

get("main_image").style.display="block"; //show the new image

 

Can anyone advise?

 

If there's a better way of vertically centralising an image within a div, that would solve it all!

 

Thanks.

If there's a better way of vertically centralising an image within a div, that would solve it all!

 

There is another method, but I think it would have the same result.

Using CSS you can center vertically. The containing div needs a height suitable for your biggest image and also needs position: relative.

 

Each image needs position: absolute; top: 50%; height: ??px; margin-top: -??px; where the negative margin-top is always half the image height:-

#container { position: relative; width: 530px; height: 200px; margin: auto; }
.image { position: absolute; top: 50%; left: 50%; width: 300px; height: 100px; margin-top: -50px; margin-left: -150px; }

 

<div id="container">
<img class="image" src="photo1" alt="Photo 1"/>
</div> 

 

See item 5 here: http://www.wickham43.net/centeringdivs.php

 

How you code that in javascript or PHP is beyond my capabilities. It would have to get the height of an image and sett the negative margin-top at half.

  • Author

Wickhan, thanks v much. I've no time this minute to try it but I'll let u know asap if it worked...

  • Author

hi

 

you were correct, it gave the same result. The only way to stop the image jumping in ie, as far as I can see, is to preload them all.

 

cheers

A different approach...

 

If you're using php you could write a function to create a separate div for each image, use absolute positioning so they stack on top of each other & hide the ones you don't want to see to start with - then use the javascript to show/hide the correct one.

 

The PHP function could also be used to assemble the javascript function passing on the div or image names as it does so.

Yet another approach is to use a graphics program to put all the images on a canvass of the same size, so that the images for the gallery are all the same size.

 

It might be possible to batch process this to save time, depending on which program you use.

  • Author

A different approach...

 

If you're using php you could write a function to create a separate div for each image, use absolute positioning so they stack on top of each other & hide the ones you don't want to see to start with - then use the javascript to show/hide the correct one.

 

The PHP function could also be used to assemble the javascript function passing on the div or image names as it does so.

 

That's a 'crafty' idea, and one I'll remember for the future... thanks.

  • Author

Yet another approach is to use a graphics program to put all the images on a canvass of the same size, so that the images for the gallery are all the same size.

 

It might be possible to batch process this to save time, depending on which program you use.

 

Also a good idea. For my gallery (http://www.ingridvekemans.com/specials/hidden-test-album) they'd all go onto a 600x600 square. The problem is that the photos are all uploaded via the CMS by my girlfriend, and the easier it is for her, the better. Now, if it was possible to load every photo automatically using PHP onto a canvas during upload, then that would be interesting!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.