▲TOPへ戻る

【javascript】画面および要素のwidth(幅)とheight(高さ)を取得する方法。

画面の幅と高さを取得する方法

  console.log(window.innerWidth); 
  console.log(window.innerHeight); 
お使いのデバイスの幅
お使いのデバイスの高さ

要素の幅と高さを取得する方法

width: 250px

height: 100px

 
  const width = document.querySelector(".box").clientWidth;
  const heght = document.querySelector(".box").clientHeght
  console.log(width); 
  console.log(height); 
width height

取得できました。

point

画面の幅:window.innerWidth

画面の高さ:window.innerHeight

要素の幅:clientWidth

要素の高さ:clientHeight

こんな記事も読まれています。

profile

パソコン好きなガオ

コロナ禍によるステイホームを機にプログラミングを学ぶ。パソコンに関してはプロではないが、ちょっと詳しい程度。

パソコン

javascript

カメラ

ブログ