Noushad Ali
Noushad Ali

Reputation: 187

LitElement how to find out width of an element

How can find the width of an HTML element in Lit-Element know, I'm unable to find to how to use @query

import { LitElement, html, query } from 'lit-element';

class MyElement extends LitElement {
  @query('#first')
  first;

  render() {
    return html`
      <div id="first">I want to know the size of this div</div>
      <div id="second">and set size to this div</div>
    `;
  }
}

Upvotes: 3

Views: 2206

Answers (1)

StephanB
StephanB

Reputation: 345

You can use window.getComputedStyle() function to get the width and any other CSS property of any element.

import { LitElement, html, query } from 'lit-element';

class MyElement extends LitElement {    
  render() {
    return html`
      <div id="first">I want to know the size of this div</div>
      <div id="second">and set size to this div</div>
    `;
  }

  updated() {
     const elem = this.shadowRoot.querySelector('#first');
     this._width = getComputedStyle(elem).getPropertyValue('width');
     console.log(this._width);
  }
}

Upvotes: 2

Related Questions