아이콘 / Javascript

html 요소의 width, height 값 구하기

펌 스크립트

펌 스크립트

2018-12-28 13:40

clientWidth, clientHeight: padding 포함 / border, scroll 비포함
offsetWidth, offsetHeight: padding, border, scroll 포함

1
2
3
4
5
6
<div id="demo" style="width:300px; height:100px; padding:10px; border:5px solid black"></div>
<script>
    var divWidth = document.getElementById('demo').offsetWidth;
    var divHeight = document.getElementById('demo').offsetHeight;
    console.log('width: ' + divWidth + ' / ' + 'height: ' + divHeight);
</script>
cs

결과: width: 330 / height: 130