Search code examples
javascriptrequestanimationframe

how to get height of an element using .offsetHeight?


I created a div element

let divContainer = document.createElement("div");
divContainer.style.height = "70%";
divContainer.id = "container";

Then, I am doing something like this...

labels.forEach(label => {
        let labelDiv = document.createElement("div");
        labelDiv.className = "label";
        labelDiv.style.height = divContainer.offsetHeight / labels.length; // here I want to retrieve the length of the divContainer in pixels.
        divContainer.appendChild(labelDiv);
    });

label is an array.

When I run this code labelDiv.style.height comes out to be 0px. I was looking for a reason for this behaviour and I found this question Element offsetHeight always "0". As suggested in one of the answers, I used the following code

requestAnimationFrame(() => {
    /* should be able to get offsetHeight here */
    console.log(divContainer.offsetHeight); 
};

and indeed I got the correct height for the label element inside the requestAnimationFrame but labelDiv.style.height is still 0 in the code given below.
I believe labelDiv.style.height is still being calculated before the code in requestAnimationFrame runs.

let divContainer = document.createElement("div");
    divContainer.style.height = "70%";
    divContainer.id = "container";


    requestAnimationFrame(() => {
        /* should be able to get offsetHeight here */
        console.log(divContainer.offsetHeight);
    });

    labels.forEach(label => {
        let labelDiv = document.createElement("div");
        labelDiv.className = "label";
        labelDiv.style.height = divContainer.offsetHeight / labels.length;
     
        divContainer.appendChild(labelDiv);
    });
  

Then. I changed the above code to this but still, I am not getting the correct output. In this case I am not again getting 0px for divContainer.offsetHeight

let divContainer = document.createElement("div");
        divContainer.style.height = "70%";
        divContainer.id = "container";


        requestAnimationFrame(() => {
            /* should be able to get offsetHeight here */
            console.log(divContainer.offsetHeight);
            labels.forEach(label => {
                let labelDiv = document.createElement("div");
                labelDiv.className = "label";
                labelDiv.style.height = divContainer.offsetHeight / labels.length;
         
                divContainer.appendChild(labelDiv);
            });
      
        });
    
       

What is wrong with the above code? What's the proper way to get the height of that element?


Solution

  • Three things:

    • you need to give body a height, since by default that's 0 (70% of 0 is 0)
    • you need to append the container to the body before iterating (70% of no parent is 0)
    • you need to add a unit to the label heights (right now it's just a number)

    let labels = ["A", "B", "C"];
    
    document.body.style.height = "500px";
    let divContainer = document.createElement("div");
    
    divContainer.style.height = "70%";
    divContainer.style.backgroundColor = "red";
    divContainer.id = "container";
    document.body.append(divContainer) 
    labels.forEach(label => {
        console.log(divContainer.offsetHeight)
        let labelDiv = document.createElement("div");
        labelDiv.className = "label";
        labelDiv.style.height = divContainer.offsetHeight / labels.length + "px";
        labelDiv.style.background = "blue";
        divContainer.appendChild(labelDiv);
    });