offsetWidth, offsetHeigth, clientWidth, clientHeight

✒️ 2025-05-08 21:11 내용 수정


참고 자료 : mdn web docs DOM Determining the dimensions of elements

js_dom_width_height.png

offsetWidth, offsetHeigth

참고 자료 : mdn web docs HTML Element offsetWidth, mdn web docs HTML Element offsetHeight

HTMLElement.offsetWidth;
HTMLElement.offsetHeight;
offsetWidth
offsetHeight
getBoundingClientRect()
반환 각각 widthheight 를 반환 여러 속성을 반환
widthheight 이 포함됨
transform 적용 시
(width=100px;
transform:scale(0.5);)
요소의 레이아웃을 반환
width = 100px;
렌더링 된 크기를 반환
width = 50px;
반환값 근사 정수로 반올림 소수점으로 반환
<div class="container"></div>
.container {
	padding: 30px;
	margin: 20px auto;
	width: 300px;
	height: 150px;
	border: 10px solid black;
	background: orange;
	overflow-y: scroll;
}
const container = document.querySelector(".container");
console.log('offsetWidth : ', container.offsetWidth);
console.log('offsetHeight : ', container.offsetHeight);
offsetWidth : 380
offsetHeight : 230
.container {
	box-sizing: border-box;
	padding: 30px;
	margin: 20px auto;
	width: 300px;
	height: 150px;
	border: 10px solid black;
	background: orange;
	overflow-y: scroll;
}
offsetWidth : 300 
offsetHeight : 150

clientWidth, clientHeight

참고 자료 : mdn web docs Element ClientWidth, mdn web docs Element ClientHeight

Element.clientWidth;
Element.clientHeight;
<div class="container"></div>
.container{
	padding: 30px;
	margin: 20px auto;
	width: 300px;
	height: 150px;
	border: 10px solid black;
	background: orange;
	overflow-y: scroll;
}
const container = document.querySelector(".container");
console.log(container.clientWidth);
console.log(container.clientHeight);
clientWidth : 345
clientHeight : 210