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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:33:28+00:00 2026-06-15T23:33:28+00:00

I am using this script to center my div horizontally and vertically. When the

  • 0

I am using this script to center my div horizontally and vertically.

When the page loads the div gets centered vertically, not horizontally until I resize the browser.

What am I doing wrong?

$(document).ready(function (){
    $(window).resize(function (){
        $('.className').css({
            position:'absolute',
            left: ($(window).width() - $('.className').outerWidth())/2,
            top: ($(window).height() - $('.className').outerHeight())/2
        });
    });
    $(window).resize();
});
  • 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-15T23:33:30+00:00Added an answer on June 15, 2026 at 11:33 pm

    I normally use this "technique":

    $(function() {
        $('.className').css({
            'position' : 'absolute',
            'left' : '50%',
            'top' : '50%',
            'margin-left' : -$('.className').width()/2,
            'margin-top' : -$('.className').height()/2
        });
    });
    

    UPDATE:

    I’m updating the solution, as suggested by the user Fred K, using .outerWidth() and .outerHeight() to have a more precise centering.

    $(function() {
        $('.className').css({
            'position' : 'absolute',
            'left' : '50%',
            'top' : '50%',
            'margin-left' : -$('.className').outerWidth()/2,
            'margin-top' : -$('.className').outerHeight()/2
        });
    });
    

    Some additional notes from jQuery’ documentation (.outerWidth(), .outerHeight()):

    • The number returned by dimensions-related APIs, including .outerWidth(), may be fractional in some cases. Code should not assume it is an integer. Also, dimensions may be incorrect when the page is zoomed by the user; browsers do not expose an API to detect this condition.

    • The value reported by .outerWidth() is not guaranteed to be accurate when the element’s parent is hidden. To get an accurate value, you should show the parent first, before using .outerWidth().


    UPDATE 2:

    A simple update to show how you could use this inside the css() method in case there are more elements with the same class tag with different sizes.

    $(function() {
        $('.className').css({
            'position' : 'absolute',
            'left' : '50%',
            'top' : '50%',
            'margin-left' : function() {return -$(this).outerWidth()/2},
            'margin-top' : function() {return -$(this).outerHeight()/2}
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this page: http://www.comehike.com/hiking.php <div style=align:center> <div id=fb-root></div> <script src=http://connect.facebook.net/en_US/all.js#appId=217585988283772&amp;xfbml=1></script> <fb:like href= send=true
I'm using this script below for loading a hidden div, inside another: $(document).ready(function(){ $(function()
in my site i want to set default page to open, using this script
I've followed the code on this link : Using jQuery to center a DIV
I've been using this script for a long time and it works perfectly in
I compress my files using this script import pylzma, struct i = open(path+fileName,'rb') o
I'm using this script which works fine for a single lightbox popup window. I
I have a file hosting site that's using this script MFHS . I want
Hay, i'm using this script function preloader(images){ var i = 0; imageObj = new
Recently I've been looking at opening a tab using this script: $('.tofour').click(function() { //

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.