I want to get the height of a div in order to make the height of another div matching it. I used the method clientHeight, but It doesn't return me the good value (smaller value). Actually, It seems to return a height before all elements are charged. After some research online, I tried to put a window.load() to delay until everything is charged but it doesn't work as well. Some ideas ?
mounted () {
this.matchHeight()
},
matchHeight () {
let height = document.getElementById('info-box').clientHeight
}
<div class="columns">
<div class="left-column" id="context">
<p>Some text</p>
</div>
<div class="right-column" id="info-box">
<img />
<ul>
some list
</ul>
</div>
</div>
I would use flexbox to achieve equal height columns https://css-tricks.com/snippets/css/a-guide-to-flexbox/
The way you are doing it is fine. But there is another vue specific way via a
ref
attribute.In this case, since you are just getting the value it really doesn't matter whether you use your original
getElementById
approach or the vue specificref
approach. However if you were setting the value on the element then it's much better to use theref
approach so that vue understands that the value has changed and won't possibly overwrite the value with the original value if it needs to update that node in the DOM.You can learn more here: https://vuejs.org/v2/api/#vm-refs
Update
A few people had left comments that the above solution didn't work for them. That solution provided the concepts but not full working code as example, so I have augmented my answer with the code below which demonstrates the concepts.
Here is a screenshot of the results in the browser: