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 9302821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:20:31+00:00 2026-06-18T23:20:31+00:00

I am using JavaScript to vertically center a child div within a fluid container.

  • 0

I am using JavaScript to vertically center a child div within a fluid container. I essentially accomplish this by calculating the height of the parent container and the height of the child div. I then absolutely positon the child div in the center of the parent div based on the height. The issue I am experiencing is that when the page loads, it does not set the position of the child div. I created a function out of this and call it when the window is resized so that when the viewport changes it recalculates dynamically. How would I go about initially setting this position on page load?

<div class="lead">
    <div class="message"></div>
</div>

var homepageLead = function () {
var lead = $('.lead'),
    message = $('.message'),
    lead_height = lead.height(),
    message_height = message.height(),
    lead_center = .5 * lead_height,
    message_center = .5 * message_height,
    center = (lead_center - message_center);    
message.css('bottom',center);
}
homepageLead();

$(window).resize(function () {
    homepageLead();
});
  • 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-06-18T23:20:32+00:00Added an answer on June 18, 2026 at 11:20 pm

    Try this fiddle: http://jsfiddle.net/nRRn6/

    used css:

    html, body {
       height:100%;
    }
    .lead {
       width:100%;
       height:100%;
       background:red;
       position:relative;
    }
    .message {
       width:120px;
       height:200px;
       background:yellow;
       position:absolute;  /* <---required*/
       left:0;             /* <---required*/
       bottom:0;           /* <---required*/
    }
    

    used html:

    <div class="lead">
        <div class="message"></div>
    </div>
    

    used jQuery:

    var homepageLead = function () {
        var lead = $('.lead'),
            message = $('.message'),
            lead_height = lead.height(),
            message_height = message.height(),
            lead_center = .5 * lead_height,
            message_center = .5 * message_height,
            center = (lead_center - message_center);
        return message.css('bottom', center);
    };
    
    $(function () {
        $(window).resize(function () {
            homepageLead();
        }).resize();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using JavaScript I open the pop window and parent window is inactive at this
I need to vertically center some images inside their container <li> blocks using CSS
How to put a div in center of browser both vertically and horizontally using
How to align a pop up division to center of monitor/screen using javascript? I
I have this div that can be dragged vertically off the screen. I want
I am trying to centre, just using css, a div element both vertically and
What I'm trying to do: Vertically center the form in the left div. The
How to center text over an image in a table cell using javascript, css,
Using javascript, How can I convert a human time string like Wed Jun 20
Using JavaScript, is it possible to use prototype to add a method to a

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.