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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:09:19+00:00 2026-05-21T23:09:19+00:00

Is there any way to get an element’s height prior to appending it to

  • 0

Is there any way to get an element’s height prior to appending it to the DOM? I know that clientHeight doesn’t work as I’ve tried and it always returns 0. Are there other methods that may return the height or does the element have to be a part of the DOM in order for the height to be calculated?

This is a sample of what I’m going for:

function test(a) {
    var a=document.createElement(a)
    a.style.top=(window.innerHeight/2-a.clientHeight/2)+'px' //fixed position in CSS
    document.body.appendChild(a)
    }

*Note: This is only a simplified version of the function I’m working on in order to project what I’m trying to achieve without all of the unneeded mess.

  • 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-21T23:09:20+00:00Added an answer on May 21, 2026 at 11:09 pm

    Elements don’t have a height, in any real sense, until they’ve been added to the DOM, as their styles cannot be evaluated until then.

    You can get around this easily enough using visibility: hidden so that the element can be added to the DOM (and its height determined) without causing visible flickering.

    function test(a) {
      var a = document.createElement(a);
      a.style.visibility = 'hidden';
      document.body.appendChild(a);
      a.appendChild(document.createTextNode('Hello'));
      a.style.fontStyle = 'italic';
      a.style.top=(window.innerHeight/2 - a.clientHeight/2) + 'px';
      a.style.visibility = '';
      return a;
    }
    
    test('p').style.background = '#0f0';
    p { position: absolute; top: 0; left: 0; }

    (This is working on the assumption that you’re using top because the element is absolutely positioned or fixed. If it weren’t, you’d need to make it so temporarily.) Hidden elements still take up space in the DOM (so their sizes must be calculated), but cannot actually be seen by the user.

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

Sidebar

Related Questions

Is there any way to get the ID of the element that fires an
Is there any way to get the height of an element if there is
Is there any way to get mouse position relative to it's parent element? Let's
Is there any accurate way to get the real size of a svg element
Is there any way to get the work item query results preview pane to
Is there any way to get a predefined element with with compass(CSS Framework)? Let's
I'm just thinking if there is any way how to get information about element
Is there any way how to get element selector which was clicked by trigger?
Is there any way to get the effect of running python -u from within
Is there any way to get the location of a cell phone (i.e. latitude/longitude)

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.