Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6674857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:46:50+00:00 2026-05-26T03:46:50+00:00

I have created a script that slides images. Each image is contained in a

  • 0

I have created a script that slides images. Each image is contained in a “slide” div. What I want to do is vertically align each individual image using jquery. Currently am I using:

$('.slide img').each(function() {
    var image_height = $(this).height();
    var height_margin = (image_height/2)*-1;
    $('.slide img').css('margin-top', height_margin);
    $('.slide img').css('top', 50%);    
    $('.slide img').css('height', image_height);    

});

What I’ve noticed is that it applies the first height and margin from the first image to ALL of the <div class"slide"></div> tags. Also: <div class"slide"></div> has the constant height of 600px.

Not every image is the same and I want it to be dynamic… Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:46:52+00:00Added an answer on May 26, 2026 at 3:46 am

    You shouldn’t use $('.slide img') inside the .each loop, because this selector will change all styles. Currently, you’re applying the settings of the last image to all images. Another error in your code: You have forgotten to quote 50%.

    $('.slide img').each(function() {
        var $this = $(this);
        var image_height = $this.height();
        var height_margin = (image_height/2)*-1;
        $this.css('margin-top', height_margin);
        $this.css('top', '50%');    
        $this.css('height', image_height);    
    });
    

    Your code can be optimized even more:

    $('.slide img').each(function() {
        var $this = $(this);
        var image_height = $this.height();
        var height_margin = -image_height / 2
        $this.css({'margin-top', height_margin,
                   'top', '50%',  
                   'height', image_height
                  });    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that create a new div element. Then, I want to
I have created a Python script that I want to run daily via a
I have created a script that saves some files in the Documents directory of
I have created a Greasemonkey script that runs fine in the firebug editor, with
I have created a PHP-script to update a web server that is live inside
I have created a dynamic list picker script using Jquery 1.3 and PHP that
I have php script that creates a temporary watermark image for users that are
I have written a CGI script that creates an image dynamically using GET data.
I have tried to create this script that would create 10 empty slides, but
I want to create a jquery script that works like the iphones slide to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.