- 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 LVM 스냅샷 실습 ext4/XFS 스냅샷 생성·변경·병합(롤백) 가이드[Linux] Rocky Linux 9.5 LVM 스냅샷 실습 ext4/XFS 스냅샷 생성·변경·병합(롤백) 가이드

1. 개요 본 글은 LVM의 스냅샷(snapshot) 기능으로 복구 지점 생성 → 파일 변경 → 스냅샷 병합(rollback) 과정을 실습합니다. 동일한…

%일 전

[Linux] Rocky Linux 9.5 LVM PV/VG/LV 구축, 온라인 확장, ext4 오프라인 축소

https://youtu.be/ZcxB7akkDKs 1. 개요 두 개의 디스크(/dev/vdb, /dev/vdc)로 LVM을 구성하고, ext4·XFS 파일시스템 생성 → 마운트 → VG/LV 확장 → ext4 축소(오프라인)까지 전체…

%일 전

[Linux] Rocky Linux 9.5 Parted로 GPT 파티셔닝: ext4/XFS 포맷과 fstab 자동 마운트

https://youtu.be/XYBR1ZFrV9s 1. 개요 parted를 사용해 새 디스크에 GPT 라벨 생성 → 파티션 생성(ext4/XFS) → 포맷/마운트 →…

%일 전

[Linux] Rocky Linux 9.5 FDISK MBR 파티셔닝 : ext4/XFS 포맷과 fstab 자동 마운트

https://youtu.be/yYV8RQKCFzA 1. 개요 이 문서는 fdisk를 사용해 MBR(DOS) 디스크에 파티션을 생성하고, ext4/XFS 파일시스템을 포맷·마운트, /etc/fstab에 등록했다가, 안전하게 해제·삭제하는 전…

%일 전

[Linux] MBR vs GPT : 리눅스 파티션 방식 쉽게 비교

1. 개요 Linux에서 디스크 파티션 스타일은 MBR(Master Boot Record) 와 GPT(GUID Partition Table)에 대해 설명합니다. 2. MBR이란? 디스크…

%일 전

[WindowsServer] Windows Server 2025 설치

https://youtu.be/CNd1bJV4wGY 1. 개요 Windows Server를 새로 설치할 때의 설치 과정을 단계별로 정리하였습니다. Windows Server 설치…

%일 전