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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:06:08+00:00 2026-06-07T03:06:08+00:00

While developing a site for many browsers, mobile and desktop, I’ve noticed that the

  • 0

While developing a site for many browsers, mobile and desktop, I’ve noticed that the following CSS works nicely to center a loading div.

img.loading1 {
    position:absolute;
    left:50%;
    margin-left:-16px;
    top:50%;
    margin-top:-16px;
    z-index:10
}
.loading2 {
    color:#006BB2;
    position:absolute;
    left:50%;
    margin-left:0px;
    top:40%;
    z-index:5
}
.loading3 {
    position:absolute;
    width:100%;
    height:100%;
    left:0;
    top:0;
    background-color:lightgrey;
    opacity:0.85
}
<div id="container" style="position:relative">
    ...content ommitted...
    <div id="loading" style="display:none">
        <div class="loading3"></div>
        <img class="loading1" src="images/loader-ajax.gif" width="32" height="32">
        <span class="loading2" id="getting">Getting your request...</span>
    </div>
    ...content ommitted...

The div’s display gets set to ‘block’ and the 3 items center great.

However, on a mobile screen, while the horizontal is right on, the vertical position could be off-screen depending on the height of the ‘containing’ div.

Is it possible to find the center of the screen and position the image and span there with Javascript?

Edit 1: Must the height of the loading div be set to be the height of the screen for this to work?

  • 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-07T03:06:10+00:00Added an answer on June 7, 2026 at 3:06 am

    Related info:

    Every absolutely-positioned element is positioned relative to a container. The default container is the body tag.

    If no dimensions are specified, an element with absolute position is shrink-wrapped to the size of its content. When calculating the size in JavaScript, the value returned is whatever the current size happens to be, based on the content it contains. The element will not have the same size as its parent unless the width or height is explicitly set to 100%.

    Without using jQuery:

    Get the x and y location of the container element (relative to the viewport), the width and height of the viewport, and the width and height of the element.

    Set the top to half the viewport height, minus the container y position, minus half the element height.
    Set the left to half the viewport width, minus the container x position, minus half the element width.

    // Center an absolutely-positioned element in the viewport.
    function centerElementInViewport(el, container) {
    
        // Default parameters
        if ((typeof container == 'undefined') || (container === null))
            // By default, use the body tag as the relative container.
            container = document.body;
    
        // Get the container position relative to the viewport.
        var pos = container.getBoundingClientRect();
    
        // Center the element
        var left = ((window.innerWidth >> 1) - pos.left) - (el.offsetWidth >> 1);
        var top = ((window.innerHeight >> 1) - pos.top) - (el.offsetHeight >> 1);
        el.style.left = left + 'px';
        el.style.top = top + 'px';
    }
    

    Here’s a jsfiddle demo. If there are problems running it in jsfiddle, try this standalone demo.

    Tested it in IE7/8/9, Firefox, Chrome, Safari, and Safari Mobile (iOS). The only thing found to cause a problem so far is if the absolutely-positioned element has a margin (which this function does not support at present).

    Haven’t tested in a responsive design yet.

    Note: Be careful not to call this function when the page first loads. If the browser was scrolled or zoomed and then reloaded, the page may not have been rescrolled or zoomed back to where it was yet, and the resulting position would be incorrect. Setting a timer of 100 msec before calling the function seemed to work (allowing the browser time to rescroll and rezoom the page).

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

Sidebar

Related Questions

I've been developing this site for a while and suddenly today I noticed that
While developing a site which I can't link to directly, I've found a following
I've noticed an issue while developing my pages that's always bothered me: while Firefox
Just wondering whats the best way to test Python CGI while developing a site?
This has been frustrating me for a while now. I started developing a site
I'm using Aptana as my IDE while developing a Symfony (1.4) site. I have
We are developing a site that has a Japanese localization. In that site, we
Editing live html while developing my site is very easy with Firebug or Chrome
I am using SVN while developing a WordPress site. Now I want to upload
I'm developing a site on a local server and a remote server. While in

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.