Reputation: 187
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
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