LoginSignup
2
2

More than 3 years have passed since last update.

z-index 使用方法

Last updated at Posted at 2019-11-18

z-index プロパティ

要素の重ね順を指定できる。

使用条件

位置が指定されている要素(position プロパティがstatic(初期値)以外)の要素に対して有効。
position: absolute; とか指定されていればOK。

<div class="red"></div>
<div class="pink"></div>
.red {
  height: 100px;
  width: 100px;
  position: absolute;
  background-color: red;
}
.pink {
  height: 100px;
  width: 100px;
  position: absolute;
  left: 20px;
  top: 20px;
  background-color: pink;
}

スクリーンショット 2019-11-18 17.33.41.png

z-index を指定していないので、後から記述したピンクボックスが前に重なっている。

.red {
  height: 100px;
  width: 100px;
  position: absolute;
  background-color: red;
  z-index: 1;
}
.pink {
  height: 100px;
  width: 100px;
  position: absolute;
  left: 20px;
  top: 20px;
  background-color: pink;
}

z-index: 1;を指定することによりレッドボックスが前に出る。
数が大きいほど手前になる。

スクリーンショット 2019-11-18 17.38.12.png

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2