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

  • Home
  • SEARCH
  • 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 6850393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:07:08+00:00 2026-05-27T01:07:08+00:00

Not being a web guy I don’t know all the quirks of HTML. I

  • 0

Not being a web guy I don’t know all the quirks of HTML. I am attempting to center some dynamically sized content inside of a div. This content should always be centered, so I hooked into window.resize to do it. I then manually call window.resize() after applying the CSS to force it to run once.

The problem is that the div does not center horizontally at first, only vertically. Subsequent window resizes force the div to center properly.

Initially I thought that I must be trying to center the div before it was ‘ready’ as I am calling it first before document.ready fires, but the vertical centering works and I added some trace statements and, sure enough, the left position is being calculated correctly, just not applied (seemingly).

Anyway, this is my first real foray into HTML+CSS, so I’m sure you guys will nail it quickly. Here is some sample html and javascript which reproduces the problem.

Also, my testing has been in Chrome exclusively thus far.

<html lang="en-US">
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>         
        <script type="text/javascript">     
            $(function() {
                $(window).resize(function() {
                    var win = $(window);
                    var area = $('#signuparea');
                    $('#signuparea').css({
                        position: 'absolute',                       
                        left: (win.width() - area.outerWidth()) / 2,
                        top: (win.height() - area.outerHeight()) / 2
                    });

                    //$('body').append('<div>l=' + ((win.width() - area.outerWidth()) / 2) + '</div>');                     
                    //$('body').append('<div>w=' + area.outerWidth() + '</div>');
                    //$('body').append('<div>h=' + area.outerHeight() + '</div>');
                });

                $(window).resize();
            });
        </script>
    </head>
    <body style="background-color:0000FF">
            <div id="signuparea">           
                <input type="password">
            </div>      
    </body>
</html>
  • 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-27T01:07:09+00:00Added an answer on May 27, 2026 at 1:07 am

    What about using this code in your window.resize event handler:

    $('#signuparea').css({
        position      : 'absolute',                       
        left          : '50%',
        top           : '50%',
        'margin-top'  : '-' + Math.round(area.height() / 2) + 'px',
        'margin-left' : '-' + Math.round(area.width() / 2) + 'px'
    });
    

    Since three of the properties set above are static they can be set in CSS:

    #signuparea {
        position : absolute;
        top      : 50%;
        left     : 50%;
    }
    

    Which makes your JS look like this:

    $(function() {
        $(window).resize(function() {
            var area = $('#signuparea');
            $('#signuparea').css({
                'margin-top'  : '-' + Math.round(area.height() / 2) + 'px',   
                'margin-left' : '-' + Math.round(area.width() / 2) + 'px',       
            });
        }).trigger('resize');
    });
    

    Here is a jsfiddle of this solution: http://jsfiddle.net/jasper/Ty6Af/2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am inheriting some ASP.NET code (I am an OS guy, not a web
Unfortunately their Wiki is down for maintenance and the web is not being helpful.
Is HttpContext.Current ever null in a web Application (assuming threads are not being used)?
My animationDidStop method is not being called for some reason, initially I thought it
I've been searching all through the web and I seem to not find any
I'm not a web guy, so this might be something really simple I'm missing,
I have a Groovy Web application which is NOT being deployed on Google app
After stopping my web app, a significant amount of PermGen is not being released.
I am not being lazy here, but just want to make sure I don't
Not being used to php, I'm facing an issue with accessing arrays and its

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.