What The Heck, z-index??
The z-index property can be a tricky little bugger. Sometimes, no matter how much you crank up the number, the element never rises to the top! In this article, we explore stacking contexts, and see how they can thwart our efforts to use z-index. We'll also
www.joshwcomeau.com
TL;DR
<style>
.box {
position: relative;
}
.first.box {
z-index: 2;
background-color: peachpuff;
}
.second.box {
z-index: 1;
margin-top: -20px;
margin-left: 20px;
}
</style>
<div class="first box"></div>
<div class="second box"></div>
<style>
header {
position: relative;
z-index: 2;
}
.tooltip {
position: absolute;
z-index: 999999;
}
main {
position: relative;
z-index: 1;
}
</style>
<header>
My Cool Site
</header>
<main>
<div class="tooltip">
A tooltip
</div>
<p>Some main content</p>
</main>
레이어와 그룹
position: relative와 z-index 같이 사용하기
.some-element {
position: relative;
z-index: 1;
}
<style>
header {
position: relative;
z-index: 2;
}
.tooltip {
position: absolute;
z-index: 999999;
}
main {
position: relative;
z-index: 1;
}
</style>
<header>
My Cool Site
</header>
<main>
<div class="tooltip">
A tooltip
</div>
<p>Some main content</p>
</main>
- The root context
-
<header>
- <main>
- <div class="tooltip">
-
시맨틱 버저닝과의 유사점
모든 사람이 Photoshop / Figma / Sketch와 같은 소프트웨어 경험이 있는 것은 아닙니다.
여러분이 더 잘 알고 있을 것 같은 또 다른 비유가 있습니다.
바로 시맨틱 버저닝 입니다.
시맨틱 버전 관리에서 버전의 다른 "계층(티어)"은 점으로 구분됩니다. (메이저.마이너.패치)
예를 들어 패키지 버전 2.0은 1.0보다 큰 버전이지만 1.999보다 큰 버전이기도 합니다.
z-인덱스는 마이너 버전과 유사하고 쌓임 맥락은 메이저 버전과 유사합니다.
쌓임 맥락이 생성될 때마다 버전에 점을 추가합니다.
<header> <!-- 2.0 -->
My Cool Site
</header>
<main> <!-- 1.0 -->
<div class="tooltip"> <!-- 1.999999 -->
A tooltip
</div>
</main>
우리의 예제 고치기
<style>
header {
position: relative;
z-index: 2;
}
.tooltip {
position: absolute;
z-index: 999999;
}
main {
position: relative;
/* No more z-index here! */
}
</style>
<header>
My Cool Site
</header>
<main>
<div class="tooltip">
A tooltip
</div>
<p>Some main content</p>
</main>

- The root context
-
<header>
-
<div class="tooltip">
-
예외사항
쌓임 맥락 만들기
- opacity를 1보다 작은 값으로 설정
- position를 fixed 또는 sticky으로 설정(이 값에는 z-index가 필요하지 않습니다!)
- normal 모드 대신 mix-blend-mode 사용
- display: flex 또는 display: grid 컨테이너 내부의 자식에 z-인덱스 추가
- transform,filter,clip-path,perspective 사용
- opacity 또는 transform과 같은 값으로 will-change 사용
- isolation:isolate를 사용하여 컨텍스트를 명시적으로 생성
몇 가지 다른 방법도 있습니다. 전체 목록은 MDN에서 찾을 수 있습니다. (the full list on MDN)
이로 인해 몇 가지 놀라운 상황이 발생할 수 있습니다. 여기에서 무슨 일이 일어나고 있는지 확인하십시오:
<style>
header {
position: relative;
z-index: 2;
}
.tooltip {
position: absolute;
z-index: 999999;
}
main {
position: relative;
/*
No more z-index…
but it's still broken??
*/
will-change: transform;
}
</style>
<header>
My Cool Site
</header>
<main>
<div class="tooltip">
A tooltip
</div>
<p>Some main content</p>
</main>
z-index에 대한 흔한 오해
<style>
.wrapper {
display: flex;
}
.second.box {
z-index: 1;
background: hotpink;
margin-top: 20px;
margin-left: -20px;
margin-right: -20px;
}
</style>
<div class="wrapper">
<div class="first box"></div>
<div class="second box"></div>
<div class="third box"></div>
</div>
잠깐...
<header>
My Cool Site
</header>
<main>
<div class="tooltip">
A tooltip
</div>
<p>Some main content</p>
</main>
믿거나 말거나 이것은 좋은 일입니다.
- 그대로, z-지수 인플레이션(거대한 z-지수 값이 계속 증가하는 추세)은 전염병입니다.
- z-색인이 있는 모든 단일 요소가 동일한 축척에 맞아야 한다면 얼마나 더 나빠질지 상상해 보십시오.
- (주: 1번부터 모든 페이지에서 차례대로 존재해야 함)
- 저는 브라우저 엔지니어는 아니지만 스택 컨텍스트가 성능에 좋다고 생각합니다.
- 그것들이 없으면 브라우저는 z-인덱스가 있는 모든 항목을 z-인덱스가 있는 다른 모든 항목과 비교해야 합니다.
- 스택 컨텍스트를 이해하면 요소를 "봉인"하는 데 유리하게 사용할 수 있습니다.
- 이것은 React와 같은 컴포넌트 중심 프레임워크에서 특히 강력한 패턴입니다.
"격리"를 사용한 완벽한 추상화 (Airtight abstractions with “isolation”)
.wrapper {
isolation: isolate;
}
- z-index 값을 처방할 필요가 없습니다.
- 정적으로 배치된* 요소에 사용할 수 있습니다.
- 어떤 식으로든 자식의 렌더링에 영향을 미치지 않습니다.
이것은 요소의 자식을 "봉인"할 수 있기 때문에 매우 유용합니다.
예를 들어 보겠습니다. 아래와 같은 봉투 컴포넌트가 있습니다.
몇개의 계층으로 되어있습니다.

function Envelope({ children }) {
return (
<div>
<BackPane style={{ zIndex: 1 }} />
<Letter style={{ zIndex: 3 }}>
{children}
</Letter>
<Shell style={{ zIndex: 4 }} />
<Flap style={{ zIndex: isOpen ? 2 : 5 }} />
</div>
)
}
function Envelope({ children }) {
return (
<div style={{ isolation: 'isolate' }}>
<BackPane style={{ zIndex: 1 }} />
<Letter style={{ zIndex: 3 }}>
{children}
</Letter>
<Shell style={{ zIndex: 4 }} />
<Flap style={{ zIndex: isOpen ? 2 : 5 }} />
</div>
)
}
position: relative; z-index: 1를 사용하여 구식 방식으로 스택 컨텍스트를 생성하지 않는 이유는 무엇입니까?
internet explorer는 isolation 못씀. transform:translate(0px) 사용
쌓임 맥락 이슈 디버깅
https://docs.microsoft.com/en-us/microsoft-edge/devtools-guide-chromium/3d-view/
Navigate webpage layers, z-index, and DOM using the 3D View tool - Microsoft Edge Development
How to use the 3D View tool, including navigating the 3D canvas and using the Composited Layers tab, Z-index tab, and DOM tab.
docs.microsoft.com
const element = document.querySelector('.tooltip');
console.log(element.offsetParent); // <main>
https://marketplace.visualstudio.com/items?itemName=felixfbecker.css-stacking-contexts
CSS Stacking Contexts - Visual Studio Marketplace
Extension for Visual Studio Code - Highlights stacking contexts in CSS and ineffective z-index declarations.
marketplace.visualstudio.com
z-context
A Chrome DevTools Extension that displays stacking contexts and z-index values in the elements panel
chrome.google.com
CSS Stacking Context inspector
Helps inspecting the css stacking contexts and solving the z-index war.
chrome.google.com
스태킹 컨텍스트 vs 레이어
https://developer.chrome.com/blog/inside-browser-part3/
Inside look at modern web browser (part 3) - Chrome Developers
Inner workings of a browser rendering engine
developer.chrome.com
'FrontEnd' 카테고리의 다른 글
리덕스 메인테이너가 추천하는 리액트 폴더 구조 - feature first (0) | 2022.08.12 |
---|---|
[publishing] 유용한 과거의 CSS와 타이포그래피 (0) | 2022.08.09 |
[3분 리액트] React18 Suspense의 Streaming SSR에 대해 알아보자 (0) | 2022.08.03 |
[3분 리액트] React의 Hydration에 대해 알아보자 (0) | 2022.08.03 |
[3분 리액트] React18의 useInsertionEffect 훅에 대해 알아보자 (0) | 2022.08.03 |