If your DIV is not displayed, you will not be able to get its size.
However, there is a workaround. Your div should be “visible”, but this does not mean that its opacity and position should be 1 and relative.
Set the opacity to 0 and the position to “absolute,” and you can get the dimensions of the DIV.
EDIT
Since I think more people will have a similar problem, I feel I have to explain my answer a little more.
If you try to get the size of the hidden element with JavaScript, you will always get 0.
So, there are methods to get the real size without displaying the item to the user. My favorite is the one I wrote about above. Here are the more detailed steps:
you set the opacity of the elements to 0. Thus, it will not be displayed to the end user until you get the dimensions.
you set the position of the element to "absolute." Thus, it does not take up space.
it’s now safe to set the display to an “inline block”.
you read the sizes of the elements. This time you will get real values.
You will return to the “hidden” screen and set the opacity and position to their original values.
And now you have the size of the hidden element.
itd
source share