10. z-index

김해준's avatar
Jan 14, 2026
10. z-index
z-index는 요소의 겹침 순서(앞/뒤) 를 결정하는 속성이다.
개념적으로는 Unity의 Order in Layer와 거의 동일하다.

📌 z-index 핵심 규칙 (가장 중요)

z-index는 position: static이 아닌 요소에서만 동작한다
즉, 아래 조건 중 하나라도 만족해야 한다.
  • position: relative
  • position: absolute
  • position: fixed
  • position: sticky
❌ position: static → z-index 무시됨
⭕ position이 지정된 요소 → z-index 적용 가능

🧠 z-index의 개념

  • z-index는 z축(깊이) 개념
  • 숫자가 클수록 앞에 보인다
  • 같은 레벨일 경우 → HTML에서 나중에 나온 요소가 위
z-index:99;/* 앞 */ z-index:1;/* 뒤 */

🧱 기본 예제 구조

notion image
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Document</title> <style> /* 깊이 1 */ .box1 { width: 300px; height: 300px; background-color: yellow; display: inline-block; } /* 깊이 1 */ .box2 { width: 300px; height: 300px; background-color: red; display: inline-block; } /* 깊이 1 */ .box3 { width: 300px; height: 300px; background-color: green; display: inline-block; } /* 깊이 2 */ .box4 { width: 300px; height: 300px; background-color: blue; display: inline-block; position: absolute; z-index: 99; } /* 깊이 1 */ .box5 { width: 300px; height: 300px; background-color: bisque; display: inline-block; position: relative; z-index: 1; } </style> </head> <body> <h1>z-index는 position이 static이 아닌애들로만 작동한다</h1> <hr /> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"></div> <div class="box5"></div> </body> </html>
각 박스는 색상만 다르고 크기는 동일하다.

1️⃣ z-index가 동작하지 않는 경우 (static)

.box1 { background-color: yellow; display: inline-block; }
  • position이 static (기본값)
  • z-index를 줘도 아무 변화 없음
👉 깊이 개념 자체가 없다

2️⃣ z-index가 동작하는 경우 (absolute)

.box4 { background-color: blue; position: absolute; z-index:99; }

결과 해석

  • .box4는 문서 흐름에서 빠짐
  • z-index가 적용됨
  • 다른 박스 위로 가장 앞에 표시
👉 position + z-index 세트가 핵심

3️⃣ relative + z-index

.box5 { background-color: bisque; position: relative; z-index:1; }
  • relative도 z-index 적용 가능
  • absolute보다 뒤에 보일 수 있음
👉 z-index는 position 종류가 아니라 숫자 비교

📐 예제 결과 정리

요소
position
z-index
결과
box1
static
없음
기본
box2
static
없음
기본
box3
static
없음
기본
box4
absolute
99
최상단
box5
relative
1
중간

⚠ 자주 하는 실수

❌ z-index만 올렸는데 안 보일 때

z-index:9999;
👉 position이 static이면 의미 없음

❌ 부모보다 자식이 앞으로 안 나올 때

  • stacking context 문제
  • 부모가 새로운 z-index 기준을 만들었을 가능성

🧠 Unity Order Layer와 비교

Unity
CSS
Order in Layer
z-index
Sorting Layer
stacking context
Canvas
position context
👉 게임 개발자라면 이해가 훨씬 빠름

🔥 자주 사용하는 z-index 쓰는 대표 사례

모달

.modal { position: fixed; z-index:1000; }

드롭다운 메뉴

.dropdown { position: absolute; z-index:10; }

툴팁

.tooltip { position: absolute; z-index:999; }

📌 z-index 규칙 정리

  • 레이아웃은 Grid / Flex
  • 겹침은 position
  • 앞뒤는 z-index
  • 숫자는 최소한으로 관리
1 ~ 10 : 일반 UI 100 : 드롭다운 1000 : 모달

✨ 요약

z-index는 position이 있는 요소의 “앞뒤 순서”를 정하는 깊이 값이다
 
Share article