CSS

[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:static 경우엔 작동 X)
  • position: absolute; 
  • position: relative;
  • position: fixed; 
  • position: sticky;
  • flex items (= display:flex 요소의 직접 자식요소들)
단, 한 요소 안 계층적 요소간엔 작동 X. (예제4) 
 
2.
  • z-index 값 없이 2개의 position 된 요소가 서로 겹칠 시, HTML 코드 순서 마지막에 위치한 요소가 더 앞에 배치됨.
  • AOS 라이브러리 사용 시 충돌 발생할 수도 있음. (예) data-aos="fade-up" 속성 제거 시 해결됨. - 카산 님 (240520) https://sir.kr/qa/534958

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 값이 가장 크지만, 자식요소가 부모요소 위에 배치 됨.



분류 제목
func CSS - var() 함수 ★★ - 사용자정의 속성/속성값 변수 호출 (= var함수 = 바함수) ※ :r…
selector CSS - element element 자손선택자 ★ - 자손요소 (요소선택자)
animation CSS - @keyframes 구문 - 애니메이션 코드 사용 선언 (= 애니사용 = @keyframes속성 …
table CSS - caption-side 속성 - 테이블표제위치 (= caption-side속성 = 캡션사이드속성)
box CSS - margin-left 속성 - 요소의 왼쪽 바깥 여백 (= 바깥 좌측 여백 = 좌측마진 = 왼쪽마…
text CSS - unicode-bidi 속성 - 텍스트글자방향 (= unicode-bidi속성 = 유니코드비디속성…
responsive CSS - @media 구문 - 미디어쿼리 이용한 반응형 스타일 구현 (= media쿼리) ※ IE/Edge…
css CSS - 선택자 명시도 계산 - (CSS 적용 선택자우선순위 판별 = CSS적용순서 체크)
text CSS - text-decoration-style 속성 - 텍스트라인모양 (굵은선, 파선, 점선, 물결선 등…
border CSS - border-image 속성 - 테두리이미지 (= border-image속성 = 보더이미지속성, …
css CSS - 이미지 세로 나열 (= 수직 정렬) 방법/경우/원인/이유
flex CSS - order 속성(I) - 아이템 순서 재조정 (= order속성 = 오더속성, IE11) ※ 플렉…
func CSS - rgb() 함수 - (빨강,녹색,파랑)으로 색상 정의. (= rgb함수 = 알지비함수)
list CSS - list-style 속성 ★ - 리스트 스타일 일괄 (= 리스트마커 = list-style속성 =…
func CSS - rgba() 함수 ★ - (빨강,녹색,파랑,불투명도)으로 색상 정의. (= rgba함수 = 알지비…
27/35
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티
웹유틸
회원센터
홈짱닷컴 PC버전 로그인