- PROGRAMMING

[HTML & CSS] CSS에서 position: absolute와 position: relative의 관계





1. position: absolute란?

CSS에서 position: absolute를 적용하면 해당 요소는 문서 흐름에서 제거되며, 특정 기준점을 기준으로 배치됩니다.
이때 기준점이 되는 요소는 가장 가까운 position이 지정된(즉, relative, absolute, fixed 중 하나인) 부모 요소입니다.


만약 부모 요소가 position: static(기본값)이라면, absolute 요소는 <html> 요소(즉, 문서 전체)를 기준으로 배치됩니다.


.parent {
  width: 300px;
  height: 300px;
  background-color: lightblue;
}

.child {
  position: absolute;
  top: 50px;
  left: 50px;
  width: 100px;
  height: 100px;
  background-color: red;
}


<div class="parent">
  <div class="child"></div>
</div>

이 경우 .child 요소는 문서 전체를 기준으로 top: 50px, left: 50px 위치에 배치됩니다.



2. 부모 요소에 position: relative를 설정하는 이유

부모 요소에 position: relative를 설정하면, 해당 부모가 자식 요소의 기준점이 됩니다.
즉, absolute 요소는 문서가 아닌 부모 요소를 기준으로 배치됩니다.


.parent {
  position: relative; /* 부모 요소를 기준점으로 만듦 */
  width: 300px;
  height: 300px;
  background-color: lightblue;
}

.child {
  position: absolute;
  top: 50px;
  left: 50px;
  width: 100px;
  height: 100px;
  background-color: red;
}


<div class="parent">
  <div class="child"></div>
</div>

이제 .child 요소는 .parent 요소를 기준으로 top: 50px, left: 50px 위치에 배치됩니다.


부모에 relative가 없으면?

부모 요소가 position: static이면 .child 요소는 <html>을 기준으로 배치됩니다. 따라서 원하는 위치에 정확히 배치하려면 부모 요소에 position: relative를 추가해야 합니다.



3. position: absolute와 position: relative의 관계 정리

자식 요소
(position: absolute)
부모 요소
(position)
기준점
absolutestatic (기본값)<html>(문서 전체)
absoluterelative부모 요소
absoluteabsolute부모 요소
absolutefixed뷰포트(화면)

즉, 부모에 relative를 주면 absolute 요소가 해당 부모를 기준으로 배치되는 것이 핵심입니다.



4. absolute 사용 시 주의할 점




4-1. 부모 요소에 height가 없을 때

부모 요소가 position: relative지만 height를 지정하지 않으면, 내부 absolute 요소가 부모의 크기를 결정하지 않으므로 의도한 대로 배치되지 않을 수 있습니다.


.parent {
  position: relative;
  background-color: lightblue;
}

.child {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 100px;
  height: 100px;
  background-color: red;
}


<div class="parent">
  <div class="child"></div>
</div>

이 경우 .parent 요소에 height가 지정되지 않으면, .child가 의도한 대로 배치되지 않을 수 있습니다.


해결 방법: 부모 요소에 height 값을 명시적으로 설정하거나, 내부 요소들의 높이를 기반으로 자동으로 설정되도록 해야 합니다.



5. absolute와 relative를 활용한 예제




5-1. 우측 하단에 버튼 고정하기

.container {
  position: relative;
  width: 300px;
  height: 300px;
  background-color: lightgray;
}

.button {
  position: absolute;
  bottom: 10px;
  right: 10px;
  padding: 10px;
  background-color: blue;
  color: white;
}


<div class="container">
  <button class="button">버튼</button>
</div>

.button 요소가 .container 요소의 오른쪽 하단에 배치됩니다.



6. 결론

  • position: absolute부모 요소가 position: relative 이상일 때 해당 부모를 기준으로 배치됩니다.
  • 부모에 position: relative가 없으면 문서 전체(<html>)를 기준으로 배치됩니다.
  • 특정 요소를 부모 기준으로 배치하려면 부모에 position: relative를 추가해야 합니다.
  • absolute 요소는 문서 흐름에서 제거되므로 부모의 크기에 영향을 주지 않습니다.



seuheu

최근 게시물

[Linux] Rocky Linux 9.5 NFS 구성 및 테스트 가이드 (rw/ro + root_squash 비교)

https://youtu.be/n8-wlkZiqio 1. 개요 NFS(Network File System)를 설치하고, rw/ro 및 root_squash 옵션에 따른 접근 제어와 성능을 테스트하는 방법을 정리한 가이드입니다.…

%일 전

[Linux] Rocky Linux 9.5 Cron 설정 및 테스트 방법

https://youtu.be/4MVxzmepY3s 1. 개요 리눅스에서 정기적으로 실행되는 작업(백업, 로그 정리, 모니터링 등)은 cron 서비스를 통해 자동화할 수 있습니다.…

%일 전

[Linux] Rocky Linux 9.5 계정 관리 (생성 · 권한 · 잠금 · 삭제) 정리

https://youtu.be/vPfxWFBE1yc 1. 개요 리눅스 서버를 운영할 때 사용자 계정 생성, 비밀번호 설정, 권한 부여, 계정…

%일 전

[Linux] Rocky Linux 9.5 Chrony로 시간 동기화 설정하기

https://youtu.be/Gvp2XwBfoKw 1. 개요 리눅스 서버에서는 시스템 시간(OS 시간) 과 하드웨어 시간(RTC, Real-Time Clock) 을 동기화하는 것이 매우 중요합니다. 클러스터…

%일 전

[Linux] Rocky Linux 9.5 리눅스 시간 관리 입문: 하드웨어(RTC)와 시스템(OS) 시간 개념부터 동기화까지

https://youtu.be/pt9qhawl8LY 1. 개요 리눅스 서버에서는 시스템 시간(OS 시간) 과 하드웨어 시간(RTC, Real-Time Clock) 을 모두 관리할 수 있습니다. 운영체제의…

%일 전

[Linux] Rocky Linux 9.5 타임존(Timezone)이 뭐예요? 리눅스 시간 확인과 변경 방법

https://youtu.be/iPdHGXh7DUg 1. 개요 서버 운영 시 시스템 시간이 올바르게 설정되어 있지 않으면 로그 분석, 모니터링,…

%일 전