z-index

z-index 속성은 요소의 쌓는 순서를 설정한다.

z-index는 position 속성이 설정된 요소, flex의 직접적인 자식 요소에서만 동작한다.

예를들어, position: absolute, position: relative, position: fixed, or position: sticky 그리고 display: flex가 설정된 요소의 직접적인 자식 요소

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
.container {
position: relative;
}

.black-box {
position: relative;
z-index: 1;
border: 2px solid black;
height: 100px;
margin: 30px;
}

.gray-box {
position: absolute;
z-index: 3; /* gray box will be above both green and black box */
background: lightgray;
height: 60px;
width: 70%;
left: 50px;
top: 50px;
}

.green-box {
position: absolute;
z-index: 2; /* green box will be above black box */
background: lightgreen;
width: 35%;
left: 270px;
top: -15px;
height: 100px;
}

z-index

만약 z-index 설정없이 두 요소가 오버랩되어 있다면, HTML 코드가 나중에 정의된 요소가 제일 위에 보여진다.