要素の幅と高さを取得するには、要素のoffsetWidth
プロパティとoffsetHeight
プロパティを使用します。
const box = document.querySelector('.box');
const width = box.offsetWidth,
height = box.offsetHeight;
Code language: JavaScript (javascript)
offsetWidth
とoffsetHeight
には、パディングとボーダーが含まれます。
ボーダーを含まない要素の幅と高さを取得するには、clientWidth
プロパティとclientHeight
プロパティを使用します。
const box = document.querySelector('.box');
const width = box.clientWidth,
height = box.clientHeight;
Code language: JavaScript (javascript)
clientWidth
プロパティとclientHeight
プロパティには、パディングも含まれることに注意してください。
このチュートリアルは役に立ちましたか?