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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:13:08+00:00 2026-06-15T14:13:08+00:00

I am trying to center a DIV in screen using below code but it

  • 0

I am trying to center a DIV in screen using below code but it is not working for me. Someone please suggest me to make it work

            var hnw = {};
            hnw.w = 0;
            hnw.h = 0;
            if (!window.innerWidth) {
                if (!(document.documentElement.clientWidth == 0)) {
                    hnw.w = document.documentElement.clientWidth;
                    hnw.h = document.documentElement.clientHeight;
                }
                else {
                    hnw.w = document.body.clientWidth;
                    hnw.h = document.body.clientHeight;
                }
            }
            else {
                hnw.w = window.innerWidth;
                hnw.h = window.innerHeight;
            }
            var midEle = document.createElement('div');
            var _x = 0;
            var _y = 0;
            var offsetX = 0;
            var offsetY = 0;
            if (!window.pageYOffset) {
                if (!(document.documentElement.scrollTop == 0)) {
                    offsetY = document.documentElement.scrollTop;
                    offsetX = document.documentElement.scrollLeft;
                }
                else {
                    offsetY = document.body.scrollTop;
                    offsetX = document.body.scrollLeft;
                }
            }
            else {
                offsetX = window.pageXOffset;
                offsetY = window.pageYOffset;
            }
            midEle.style.width = "300px";
            midEle.style.height = "300px";
            midEle.innerHTML = "Some Text";
            midEle.style.display = "block";
            var _x_w = parseInt(midEle.style.width, 10), _y_h = parseInt(midEle.style.height, 10);
            _x = ((hnw.w - _x_w) / 2) + offsetX;
            _y = ((hnw.h - _y_h) / 2) + offsetY;
            console.log(_x, " ", _y);
            midEle.style.position = "absolute";

            midEle.style.left = _x;
            midEle.style.top = _y;
            document.body.appendChild(midEle);
  • 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-15T14:13:09+00:00Added an answer on June 15, 2026 at 2:13 pm

    Can’t this be done with CSS instead?

    Javascript:

    var midEle = document.createElement('div');
    midEle.className = 'centered';
    document.body.appendChild(midEle);
    

    ​CSS:

    ​.centered{ 
        background-color: red;
        height: 300px;
        width: 300px;
        margin: -150px 0 0 -150px;
        top: 50%;
        left: 50%;
        position: fixed;
    }​
    

    http://jsfiddle.net/FkEyy/3/

    By setting the margins to the negative half of the elements height and width, and setting top and left values to 50%, you’ll center the element. This is a common way of centering content with CSS 🙂

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

Sidebar

Related Questions

I am trying to center a div button and its not working here is
I'm trying to center a map on a LatLng I specify, but I'm not
I am trying to center a div #logo_alt containing an image using margin: 40px
Im trying to align a jquery popup div to the center of my screen
I'm trying to center a div on the document, and it works horizontally, but
I'm still using 4.1 and I'm trying to center a form content on screen.
I'm trying to center a div in my page but I have a very
I'm trying to make a div for my landing page of my website center
I'm trying to vertically center a div in the viewable area on a very
I'm trying to horizontally and vertically center a modal window inside a div. I

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.