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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:10:29+00:00 2026-05-27T02:10:29+00:00

This is a followup to my question here . I would like to understand

  • 0

This is a followup to my question here. I would like to understand why applying position:absolute to the CSS of a div via jQuery fails, while applying it in a static style works. Here are two jsfiddle examples:

Works: http://jsfiddle.net/jasper/Ty6Af/2/

No worky: http://jsfiddle.net/Ty6Af/3/

Note that the only difference between the two is where I apply position:absolute. Vertical centering always works, but horizontal centering does not work when the page loads for the first time. If you manually re-size the window the div will center correctly.

All of my testing has been on Chrome under Ubuntu thus far.

Anyway, I’m just now delving into the world of web development and these are exactly the kinds of ‘quirks’ that I need to begin understanding.

EDIT:

@Jasper found something interesting. If you make two calls to .css(), first applying position and subsequently applying a margin, it works. I would love to understand why. Here is an example: http://jsfiddle.net/jasper/Ty6Af/5/

  • 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-27T02:10:29+00:00Added an answer on May 27, 2026 at 2:10 am

    So the issue is with how the width of the div is calculated by the browser depending on its position.

    • If the div is set to position : static (by default) then it’s width is 100% of it’s parents width and the element is not allowed to move around the page.
    • If the div is set to position : relative then it’s width is 100% of it’s parents width but it can be moved around with left.
    • If the div is set to position : absolute then its width is determined by the actual content of the div, for instance if there is only a 200px wide <span> element within the div then the div will be 200px wide.

    You can test these observations by changing the CSS of your jsfiddle to specify position : relative (etc…) and remove the JavaScript that makes the div position : absolute, then use your Developer Tools to inspect the element and it’s calculated width.

    The problem with your code is that it sets the position : absolute at the same time it sets the margin of the element by using its width/height (which are calculated differently depending on the position of the element).

    If you want to set the position of the div in JavaScript then you can do something like this:

    $(function() {
    
        //notice I cached the selector so it can be used in the future as well as set the position of the div
        $signuparea = $('#signuparea').css({position : 'absolute'});
        $(window).resize(function() {
            $signuparea.css({
                'margin-top'  : '-' + Math.round($signuparea.height() / 2) + 'px',   
                'margin-left' : '-' + Math.round($signuparea.width() / 2) + 'px',       
            });
        }).trigger('resize');
    });
    

    Here’s a jsfiddle: http://jsfiddle.net/jasper/Ty6Af/8/

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

Sidebar

Related Questions

This is a followup to this question. Here's the code I'm trying to understand
This is a followup question to my other widget-related question . I'd like to
This is a followup question for this: Scrollpane on the bottom, css is hacky,
This is somewhat of a followup to related question I found here . In
This is a follow-up to my other question here: How to read vfat attributes
This question is sort of a follow-up to my original question here . Let's
Just a follow up question to this one here => link Is it possible
This is a followup question to my other question : Run bat file in
This is a followup on the question: ASP.NET next/previous buttons to display single row
This is a followup to a question I posted yesterday. I thought everything was

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.