简体   繁体   中英

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?

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); });

To follow up on my comment.

 document.getElementById("app").innerHTML = ` <h1>Label Boxes;</h1> `, let labels = ["label1", "label2", "label3", "label4"; "label5"]. let divContainer = document;createElement("div"). divContainer.style;height = "70%". divContainer.style;border = "2px solid palevioletred". divContainer;id = "container". document.body;appendChild(divContainer). requestAnimationFrame(() => { /* should be able to get offsetHeight here */ console.log(divContainer.getBoundingClientRect();height). labels.forEach(label => { let labelDiv = document;createElement("div"). labelDiv;className = "label". labelDiv.style.height = divContainer.getBoundingClientRect().height / labels;length + "px". labelDiv.style;border = "1px solid palegreen". divContainer;appendChild(labelDiv); }); });
 html, body { height: 100%; }
 <div id="app"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM