offsetWidth vs getBoundingClientRect

yoeubi
3 min readApr 12, 2020

--

실제 엘리먼트의 크기를 가져오는 프로퍼티의 차이를 알아보겠습니다.

offsetWidth

offsetWidth는 엘리먼트의 레이아웃 width(정수형)를 리턴하는 읽기 전용 프로퍼티 입니다.

가상 선택자인 before, after를 제외한 모든 CSS width, border, padding, scrollbar등을 포함해서 계산을 합니다.

만약 엘리먼트가 display: none 이거나 부모가 none 이면 0을 리턴합니다.

getBoundingClientRect

뷰포트에서 상대적인 엘리먼트의 크기를 리턴합니다.

엘리먼트 크기는 width, height + padding 과 같습니다.

width, height는 box-sizing이 border-box일 경우에만 세팅이 됩니다.

offsetWidth vs getBoundingClientRect

두 프로퍼티의 차이는 offsetWidth는 정수형 값으로 반올림하며 getBoundingClientRect는 소수점까지 값을 리턴합니다.

만약 transform 속성을 적용했다면 offsetWidth는 엘리먼트의 레이아웃 width를 리턴하지만 getBoundingClientReact는 렌더링된 width, height를 리턴합니다.

예를들어 width: 100px에 transform: scale(0.5)를 적용했다면 offsetWidth는 100을 리턴하지만 getBoundingClientRect는 width:50으로 리턴합니다.

참고 링크

MDN offsetWidth

MDN getBoundingClientRect

Understanding offsetWidth, clientWidth, scrollWidth and -Height, respectively

JavaScript getBoundingClientRect() vs offsetHeight while calculate element height

MDN Determining the dimensions of elements

Measuring Element Dimension and Location with CSSOM in Windows Internet Explorer 9

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

yoeubi
yoeubi

Written by yoeubi

Junior Frontend engineer

No responses yet

Write a response