Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. CSS
  3. CSS positioned layout
  4. Using z-index

Using z-index

The first article of this guide,Stacking without thez-index property, explains how stacking is arranged by default. If you want to create a custom stacking order, you can use thez-index property on apositioned element.

Thez-index property can be specified with an integer value (positive, zero, or negative), which represents the position of the element along an imaginary z-axis. If you are not familiar with the term 'z-axis', imagine the page as a stack of layers, each one having a number. Layers are rendered in numerical order, with larger numbers above smaller numbers (X represents an arbitrary positive integer):

LayerDescription
Bottom layerFarthest from the observer
Layer -XLayers with negativez-index values
Layer 0Default rendering layer
Layer XLayers with positivez-index values
Top layerClosest to the observer

Note:

  • When noz-index property is specified, elements are rendered on the default rendering layer (Layer 0).
  • If several elements share the samez-index value (i.e., they are placed on the same layer), stacking rules explained in the sectionStacking without thez-index property apply.

Example

In this example, the layers' stacking order is rearranged usingz-index. Thez-index of DIV #5 has no effect since it is not a positioned element.

HTML

html
<div>  <strong>DIV #1</strong>  <br />position: absolute; <br />z-index: 5;</div><div>  <strong>DIV #2</strong>  <br />position: relative; <br />z-index: 3;</div><div>  <strong>DIV #3</strong>  <br />position: relative; <br />z-index: 2;</div><div>  <strong>DIV #4</strong>  <br />position: absolute; <br />z-index: 1;</div><div>  <strong>DIV #5</strong>  <br />no positioning <br />z-index: 8;</div>

CSS

css
div {  padding: 10px;  opacity: 0.7;  text-align: center;}strong {  font-family: sans-serif;}#abs1 {  z-index: 5;  position: absolute;  width: 150px;  height: 350px;  top: 10px;  left: 10px;  border: 1px dashed #990000;  background-color: #ffdddd;}#rel1 {  z-index: 3;  height: 100px;  position: relative;  top: 30px;  border: 1px dashed #669966;  background-color: #ccffcc;  margin: 0px 50px;}#rel2 {  z-index: 2;  height: 100px;  position: relative;  top: 15px;  left: 20px;  border: 1px dashed #669966;  background-color: #ccffcc;  margin: 0px 50px;}#abs2 {  z-index: 1;  position: absolute;  width: 150px;  height: 350px;  top: 10px;  right: 10px;  border: 1px dashed #990000;  background-color: #ffdddd;}#sta1 {  z-index: 8;  height: 70px;  border: 1px dashed #999966;  background-color: #ffffcc;  margin: 0px 50px;}

Result

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp