• 회원가입
  • 로그인
  • 구글아이디로 로그인

[position] CSS - z-index 속성 ★ - 요소층수지정 (= 요소상하지정 = 요소상하결정 = 위아래지정 = z-index속성 = 지인덱스속성/제트인덱스속성, 상속 X)

목차
  1. z-index 정의
  2. z-index 구문
  3. z-index 예제1 - 요소가 1개일 땐 무의미
  4. z-index 예제2 - 4번째가 가장 위에 위치
  5. z-index 예제3 - 3번째가 가장 위에 위치
  6. z-index 예제4 - 부모자식 계층적 요소간엔 작동 X

 

z-index 정의 

 
요소끼리 겹칠 때 상하 지정. (= 요소간 상대적 층간 위치 지정.)
즉,
몇 개의 요소가 position 속성에 의해 위치 겹칠 때,
z-index 숫자가 더 높은 요소가 더 앞쪽에 노출 됨.

 


 
1. 
position 속성과 함께 사용. ★★★★★
즉, 아래 경우에 적용됨.
  • position: absolute; 
  • position: relative;
  • position: fixed; 
  • position: sticky;
  • flex items (= display:flex 요소의 직접 자식요소들)
단, 한 요소 안 계층적 요소간엔 작동 X. (예제4) 
 
2.
z-index 값 없이 2개의 position 된 요소가 서로 겹칠 시,
HTML 코드 순서 마지막에 위치한 요소가 더 앞에 배치됨.

3. 
  • 기본값: auto
  • 상속여부: X
  • 애니효과: O
  • CSS버전: CSS2
  • JS구문 : object.style.zIndex="-1"
 
4.
모든 브라우저 지원.
 
5. MDN z-index 예제보기
 
 

z-index 구문

 
selector {z-index: auto|number|initial|inherit;}
 

[속성값]
 
auto
문서 흐름에 따른 기본 위치에 위치 (기본값)
 
number
숫자 클수록 겹쳤을 때 위에 위치. 
  • 음수 가능. 이 경우 다른 요소들보다 밑면에 위치.
 
initial
이 속성의 기본값으로 설정.
 
inherit
부모요소 속성값 상속.

 

z-index 예제1 - 요소가 1개일 땐 무의미

 
<style>
#layer1 {
     background-color:red;
     width:200px; height:200px;
     position:absolute;
     top:106px;
     left:209px;
     z-index:1;
}
</style>
 
<div id="layer1">홈짱닷컴 (homzzang.com)</div>
 
 

z-index 예제2 - 4번째가 가장 위에 위치

 
 
<style>
div {
  width:200px; 
  height:200px; 
  position:absolute;   
}
#layer1 {
  background-color:red; 
  left:209px; 
  top:106px; 
  z-index:1;
}
#layer2 {
  background-color:blue; 
  left:265px; 
  top:142px; 
  z-index:2;
}
#layer3 {
  background-color:magenta; 
  left:322px; 
  top:197px; 
  z-index:3;
}
#layer4 {
  background-color:yellow; 
  left:379px; 
  top:247px; 
  z-index:4;
}
</style>
 
<div id="layer1"></div>
<div id="layer2"></div>
<div id="layer3"></div>
<div id="layer4"></div>
 

z-index 예제3 - 3번째가 가장 위에 위치

 
 
<style>
div {
  width:200px; 
  height:200px; 
  position:absolute;   
}
#layer1 {
  background-color:red; 
  left:209px; 
  top:106px; 
  z-index:1;
}
#layer2 {
  background-color:blue; 
  left:265px; 
  top:142px; 
  z-index:2;
}
#layer3 {
  background-color:magenta; 
  left:322px; 
  top:197px; 
  z-index:4;
}
#layer4 {
  background-color:yellow; 
  left:379px; 
  top:247px; 
  z-index:3;
}
</style>
 
<div id="layer1"></div>
<div id="layer2"></div>
<div id="layer3"></div>
<div id="layer4"></div>
 

 

z-index 예제4 - 부모자식 계층적 요소간엔 작동 X


<style>

div {width:100px; height:100px; position:absolute;}

.hz {background:white; top:100px; left:100px; }

.a {background:red; top:0px; left:0px; z-index:3}

.b {background:blue; top:30px; left:30px; z-index:2}

.c {background:green; top:30px; left:30px; z-index:1}

</style>

<div class="hz">
    <div class="a">a
         <div class="b">b
              <div class="c">c</div>
         </div>
    </div
</div>

결과 보기

※ 주의: a요소의 z-index 값이 가장 크지만, 자식요소가 부모요소 위에 배치 됨.


방문 감사합니다. (즐겨찾기 등록: Ctrl + D)

분류 제목
background CSS - background-size 속성 ★ - 배경이미지 크기 (IE9) ※ 배경이미지 중첩
color CSS - RGBA , HSL , HSLA , opacity - (색상코드)
gradient CSS - Gradient (그레이디언트 = 그라디언트) 종류 - (색번짐변화=색변화) : IE 10
shadow CSS - text-shadow 속성 - 글자 그림자/음영 효과 주기 (= text-shadow속성 = 텍스…
shadow CSS - box-shadow 속성 - 요소 그림자 효과 주기 (= box-shadow속성 = 박스섀도속성)…
text CSS - text-align-last 속성 - 단락마지막라인정렬 (= text-align-last속성 = …
text CSS - text-emphasis 속성 - 텍스트 강조마크의 '모먕/색깔' 일괄 지정 (= 텍스트 글자 위…
text CSS - text-justify 속성 - 텍스트 좌우균분정렬 세부설정 (= text-justify속성 = …
text CSS - text-overflow 속성 ★ - 영역 이탈 글자 처리 (= text-overflow속성 = …
text CSS - word-break 속성 ★ - 한중일 외의 언어 (단어기준/글자기준) 줄바꿈규칙 지정 (= 줄…
text CSS - word-wrap 속성 ★ - 단어줄바꿈 위해 긴단어쪼개기 (= 긴단어깨기 = 긴단어나누기 = w…
font CSS - @font-face 구문 - 다운받은 웹폰트적용 (= @font-face쿼리 = 다운글꼴적용 = …
transform CSS - transform 속성 ★ - 요소 (회전/비틀기/크기확대) 변환 + 마우스 허버 시 줌효과 (=…
transform CSS - transform-origin 속성 - 변형요소 위치 변경. ( = transform-origin…
transform CSS - transform-style 속성 - 중첩요소를 3D공간에서 표시 방법 (= transform-s…
transform CSS - perspective 속성 - 3D요소의 원근 조망. (= perspective속성 = 퍼스펙티브…
transform CSS - perspective-origin 속성 - 3D요소 바라보는 위치 (= perspective-or…
transform CSS - backface-visibility 속성 - 3D요소 뒷면 노출 여부. (= backface-vi…
transition CSS - transition 속성 ★★★ - 지정시간 동안 천천히 변화 (= transition속성 = 트…
transition CSS - transition-delay 속성 - 트랜지션 시작 대기 시간 (= transition-dela…
8/25
목록
찾아주셔서 감사합니다. Since 2012