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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:56:24+00:00 2026-05-23T15:56:24+00:00

I had a problem where I am position a popup <div> in the center

  • 0

I had a problem where I am position a popup <div> in the center of the window using:

var popup = $("#popup"), popupWidth = popup.css("width").replace("px",""), popupHeight = popup.css("height").replace("px","");
    var xPosition = ($(window).width() - popupWidth) / 2;
    var yPosition = (($(window).height() - popupHeight) / 2) + $(window).scrollTop();

    if (yPosition <= 0){
        yPosition = '0'; 
    } else if(yPosition <= $(window).scrollTop()){
        yPosition = $(window).scrollTop();
    } else {
        yPosition = yPosition - 68; //minus top shaddow height
    }           

    if (xPosition >= $('body').offset().left) {
        xPosition = xPosition;
    } else {
        xPosition = '0';
    }

    $(popup).css({
        'top': yPosition + 'px',
        'left': xPosition + 'px',
        'display' : 'block',
        'height' : 'auto'
    }).addClass("popup-open");

The problem I had was that on first load the height of the popup was being returned as 0 because it is hidden until after the position above has been worked out. To resolve this I set a default height via CSS and then once the popup has been displayed I overwrite this to auto.

The problem now is that if the popup has been closed and re-opened the height is auto. Is there a way to find the CSS height:value in the stylesheet not the inline height:auto

Updated Code
Following Nicola answer here is the fixed code:

var popup = $("#popup"), popupWidth = popup.css("width").replace("px",""), popupHeight = popup.css("height").replace("px","");

// Save/Get original height
if(popupHeight == "auto"){
    popupHeight = popup.data('origHeight');
} else {
    popup.data('origHeight', popupHeight);
}

var xPosition = ($(window).width() - popupWidth) / 2;
var yPosition = (($(window).height() - popupHeight) / 2) + $(window).scrollTop();
  • 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-23T15:56:24+00:00Added an answer on May 23, 2026 at 3:56 pm

    Why don’t you save the original values in a variable?

    var popup = $("#popup"), popupWidth = popup.css("width").replace("px",""), popupHeight = popup.css("height").replace("px","");
    
    var originalHeight = popupHeight;
    var originalWidth = popupWidth;
    

    When you reopen the pop up you use the original values instead of the actual values

    Or you could use use data() to store it and retrieve it

     $('#popup').data('origHeight', popupHeight);
     //the use  $('#popup').data('origHeight') to retrieve it
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble with displaying a popup div with CSS. The problem is
I have this function inside a $(document).ready $('.roundabout-in-focus').click(function(){ $(this).css({position:'absolute',height:'300px',width:'400px',left:'120px',top:'-20px'}); }); Which works fine, The
I recently had a little problem using a jquery plugin with coffeescript (within the
Has anybody else had problem with position() working odd in IE? I'm trying to
I got a floating header div that follows my scroll position. my problem is
Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I had a problem that was partially solved. To explain it quickly : I
I had a problem with committing changes after merging two branches of my project
I had this problem before and can't for life of me remember how to
I had no problem at all running the following code on a local server,

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.