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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:01:59+00:00 2026-05-13T08:01:59+00:00

I need to position an element in the exact centre of the page (using

  • 0

I need to position an element in the exact centre of the page (using fixed positioning) and need to calculate the expected size. For example I would add HTML like this:

<div class="window" style="left: X; top: Y">
    <h3 class="title">Title</h3>
    <img src="file.jpg" width="300" height="200" />
    <div class="controls">Some stuff</div>
</div>

The width will be the image width plus some padding. I want to determine what the height of the element will be by getting the styles from the stylesheet, then calculate the correct position, then write the HTML with the right styles.

  • 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-13T08:01:59+00:00Added an answer on May 13, 2026 at 8:01 am

    You can only tell once it’s been added to the document (that is, you can’t tell the expected size and you won’t be able to do it server-side).

    So, in Javascript, set the visibility to hidden, add it to the document, check its outerHeight or with jQuery call $('.window').height(), reposition as needed, and then change visibility back to visible.

    …or, use this plugin:

    jQuery(function($) {
        $.fn.vCenter = function(options) {
            var empty = {},
                defaults = {
                    allowNegative : false,
                    relativeToBody : true
                },
                settings = $.extend(empty, defaults, options)
            ;
    
            var isVisible = this.is(":visible");
            if (!isVisible) {
                this.css({visibility : "hidden"}).show();
            }
            if (settings.relativeToBody && this.offsetParent().get(0).nodeName.toLowerCase() != "body") {
                this.appendTo(document.body);
            }
            var pos = {
                sTop : function() {
                    return window.pageYOffset || $.boxModel && document.documentElement.scrollTop || document.body.scrollTop;
                },
                wHeight : function() {
                    if ($.browser.opera || ($.browser.safari && parseInt($.browser.version, 10) > 520)) {
                        return window.innerHeight - (($(document).height() > window.innerHeight) ? getScrollbarWidth() : 0);
                    } else if ($.browser.safari) {
                        return window.innerHeight;
                    } else {
                        return $.boxModel && document.documentElement.clientHeight || document.body.clientHeight;
                    }
                }
            };
            return this.each(function(index) {
                if (index === 0) {
                    var $this = $(this),
                        $w = $(window),
                        topPos = ($w.height() - $this.height()) / 2 + $w.scrollTop()
                    ;
                    if (topPos < 0 && !settings.allowNegative) topPos = 0;
    
                    $this.css({
                        position: 'absolute',
                        marginTop: '0',
                        top: topPos //pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2)
                    });
                    if (!isVisible) {
                        $this.css({visibility : ""}).hide();
                    }
                }
            });
        };
        $.fn.hCenter = function(options) {
            var empty = {},
                defaults = {
                    allowNegative : false,
                    relativeToBody : true
                },
                settings = $.extend(empty, defaults, options)
            ;
    
            if (settings.relativeToBody && this.offsetParent().get(0).nodeName.toLowerCase() != "body") {
                this.appendTo(document.body);
            }
            return this.each(function(index) {
                if (index === 0) {
                    var $this = $(this),
                        $d = $(document),
                        leftPos = ($d.width() - $this.width()) / 2 + $d.scrollLeft()
                    ;
                    if (leftPos < 0 && !settings.allowNegative) leftPos = 0;
                    $this.css({
                        position: "absolute",
                        left: leftPos
                    });
                }
            });
        };
        $.fn.hvCenter = function(options) {
            return this.vCenter(options).hCenter(options);
        };
    });
    

    Usage:

    $('.window').hvCenter();  // horizontal and vertical center
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find out the pixel position of one element in a list
I need to position this update button on www.euroworker.no/order (you'll have to add an
I need to find an element position in an std::vector to use it for
I need to position each element of my HTML independently to each other i.e.
I need to retrieve the X & Y position of an element once its
I need to get position of element relative to its parent. position() is suppose
I need find element position in list (as the built-in predicate nth, but here
I need to position something with absolute positioning inside a td. To get around
I need to change the position of an element i'm loading with ajax .
I need to find a specific elements position in a jQuery collection. For example

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.