Categories
Vue Answers

How to Get an Element’s Height with Vue.js?

Spread the love

Sometimes, we want to get an element’s height with Vue.js.

In this article, we’ll look at how to get an element’s height with Vue.js.

Get an Element’s Height with Vue.js

To get an element’s height with Vue.js, we can assign a ref to the element we want to get the height for.

Then we can get the height with the clientHeight property of the element that’s been assigned the ref.

For instance, we can write:

<template>  
  <div id="app">  
    <div ref="infoBox">hello world</div>  
  </div>  
</template>  
<script>  
export default {  
  name: "App",  
  mounted() {  
    console.log(this.$refs.infoBox.clientHeight);  
  },  
};  
</script>

We assigned the ref with the ref prop set to a name.

Then we can use the this.$refs property to get the element with thos.$refs.infoBox which returns the div element.

And then we can use clientHeight to get the div’s height.

Conclusion

To get an element’s height with Vue.js, we can assign a ref to the element we want to get the height for.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *