πŸͺ„ Programming

[CSS] μŒ“μž„ λ§₯락 Stacking Context

ColorFilter 2025. 2. 26. 23:17
λ°˜μ‘ν˜•

TL;DR


  • 기본적으둜 μš”μ†Œλ“€μ€ DOM μˆœμ„œμ— 따라 μŒ“μΈλ‹€.
  • positioned(position ≠ static) μš”μ†ŒλŠ” non-positioned(position = static) μš”μ†Œλ³΄λ‹€ μœ„μ— μŒ“μΈλ‹€.
  • positioned μš”μ†Œλ“€ 쀑 z-index 값이 λ†’μ„μˆ˜λ‘ μœ„μ— μŒ“μΈλ‹€.
  • μƒˆλ‘œμš΄ μŒ“μž„ λ§₯락이 μƒμ„±λ˜λ©΄ μžμ‹ μš”μ†ŒλŠ” ν•΄λ‹Ή λ§₯락 μ•ˆμ—μ„œ μˆœμ„œκ°€ κ²°μ •λœλ‹€.

 

 

μ„€λͺ…


μŒ“μž„ λ§₯락(Stacking Context)은 HTML μš”μ†Œλ“€μ„ κ°€μƒμ˜ zμΆ•(깊이) λ°©ν–₯으둜 λ°°μΉ˜ν•˜λŠ” κ°œλ…μ΄λ‹€. 기본적으둜 μš”μ†Œλ“€μ€ DOM μˆœμ„œμ— 따라 μŒ“μΈλ‹€. 즉, 뒀에 μ •μ˜ν•œ μš”μ†ŒμΌμˆ˜λ‘ 화면에선 μœ„μ— μœ„μΉ˜ν•œλ‹€.

 

position을 μ μš©ν•œ μš”μ†Œ(positioned)λŠ” position을 μ μš©ν•˜μ§€ μ•Šμ€ μš”μ†Œ(non-positioned)보닀 μœ„μ— μŒ“μΈλ‹€. positioned μš”μ†Œλ“€ 쀑에선 z-index 값이 λ†’μ„μˆ˜λ‘ μœ„μ— μŒ“μΈλ‹€. 참고둜 z-indexλŠ” positioned μš”μ†Œμ—μ„œλ§Œ μž‘λ™ν•œλ‹€. 

 

  • positioned: position 속성이 static μ΄μ™Έμ˜ 값을 κ°€μ§€λŠ” μš”μ†Œ
  • non-positioned: position 속성이 static인 μš”μ†Œ
  • position 속성을 μ§€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ κΈ°λ³Έκ°’ static (non-positioned)
  • z-index 속성을 μ§€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ κΈ°λ³Έκ°’ auto

 

μ•„λž˜ 쑰건을 λ§Œμ‘±ν•˜λŠ” μš”μ†ŒλŠ” μƒˆλ‘œμš΄ μŒ“μž„ λ§₯락을 μƒμ„±ν•œλ‹€. 그럼 μžμ‹ μš”μ†Œλ“€μ€ ν•΄λ‹Ή λ§₯락 λ‚΄μ—μ„œλ§Œ μŒ“μž„ μˆœμ„œκ°€ κ²°μ •λΌμ„œ λΆ€λͺ¨μ˜ μŒ“μž„ λ§₯락을 λ²—μ–΄λ‚  수 μ—†κ²Œ λœλ‹€. λ•Œλ¬Έμ— μžμ‹ μš”μ†Œμ˜ z-indexκ°€ λΆ€λͺ¨ μš”μ†Œλ³΄λ‹€ 크더라도 λΆ€λͺ¨ μš”μ†Œκ°€ λ‹€λ₯Έ μš”μ†Œλ“€λ³΄λ‹€ 뒀에 μžˆλ‹€λ©΄ μžμ‹ μš”μ†Œ μ—­μ‹œ 뒀에 λ°°μΉ˜λœλ‹€.

 

  1. position: fixed ν˜Ήμ€ position: sticky
  2. position: relative ν˜Ήμ€ position: absolute μ΄λ©΄μ„œ, z-index 속성이 autoκ°€ 아닐 λ•Œ
  3. display: flex ν˜Ήμ€ display: grid μ΄λ©΄μ„œ, z-index 속성이 autoκ°€ 아닐 λ•Œ
  4. opacity 값이 1 미만일 λ•Œ
  5. transform, filter, backdrop-filter λ“± 속성이 none이 아닐 λ•Œ

 

 

μ˜ˆμ‹œ


<div style="position: relative; z-index: 1">
  A μš”μ†Œ (z-index: 1)
  <div style="position: absolute; z-index: 999">
    A-1 μš”μ†Œ (z-index: 999)
  </div>
</div>

<div style="position: relative; z-index: 2">
  B μš”μ†Œ (z-index: 2)
</div>

 

 

  1. μ΅œμƒμœ„ μŒ“μž„ λ§₯락: B μš”μ†Œ z-index > A μš”μ†Œ z-index μ΄λ―€λ‘œ B μš”μ†Œκ°€ μœ„μ— μŒ“μž„
  2. A μš”μ†Œ λ‚΄λΆ€μ˜ μŒ“μž„ λ§₯락: A-1 μš”μ†Œ z-index > A μš”μ†Œ z-index μ΄λ―€λ‘œ A-1 μš”μ†Œκ°€ μœ„μ— μŒ“μž„
  3. μ΅œμ’…μ μœΌλ‘œ (μ•„λž˜μͺ½) AA-1B (μœ„μͺ½) μˆœμ„œλ‘œ μŒ“μž„

 

 

레퍼런슀


 

μŒ“μž„ λ§₯락 - CSS: Cascading Style Sheets | MDN

μŒ“μž„ λ§₯락(stacking context)은 κ°€μƒμ˜ Z좕을 μ‚¬μš©ν•œ HTML μš”μ†Œμ˜ 3차원 κ°œλ…ν™”μž…λ‹ˆλ‹€. Z좕은 μ‚¬μš©μž 기쀀이며, μ‚¬μš©μžλŠ” 뷰포트 ν˜Ήμ€ μ›ΉνŽ˜μ΄μ§€λ₯Ό 바라보고 μžˆμ„ κ²ƒμœΌλ‘œ κ°€μ •ν•©λ‹ˆλ‹€. 각각의 HTML μš”μ†ŒλŠ”

developer.mozilla.org

 


κΈ€ μˆ˜μ •μ‚¬ν•­μ€ λ…Έμ…˜ νŽ˜μ΄μ§€μ— κ°€μž₯ λΉ λ₯΄κ²Œ λ°˜μ˜λ©λ‹ˆλ‹€. 링크λ₯Ό μ°Έκ³ ν•΄ μ£Όμ„Έμš”
λ°˜μ‘ν˜•