4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

擬似要素(::after, ::before)のz-indexが思い通りにいかない場合の対処法

Posted at

#結論
###z-indexをマイナスの値にする。

#z-indexがプラスの値、または0の場合

####CSS

body {
    margin: 0;
}

.hello {
    position: relative;
    font-weight: bold;
    font-size: 60px;
    line-height: 1;
    padding-left: 10px;
    z-index: 1;
}

.hello::after {
    position: absolute;
    content: "";
    top: 30px;
    left: 0;
    width: 180px;
    height: 35px;
    background: lightgreen;
    z-index: 0; /* ←ここが原因 */
}

###↓
スクリーンショット 2021-02-04 17.06.20.png

#z-indexがマイナスの値の場合

####CSS

body {
    margin: 0;
}

.hello {
    position: relative;
    font-weight: bold;
    font-size: 60px;
    line-height: 1;
    padding-left: 10px;
    z-index: 1;
}

.hello::after {
    position: absolute;
    content: "";
    top: 30px;
    left: 0;
    width: 180px;
    height: 35px;
    background: lightgreen;
    z-index: -1; /* ←マイナスの値にする */
}

###↓
スクリーンショット 2021-02-04 17.27.34.png

#参考URL
z-index :after :before で装飾したとき親要素を一番上にする
http://noanoa.bangofan.com/html%E3%80%81css/z-index%20-after%E3%80%80-before%E3%80%80%E3%81%A7%E8%A3%85%E9%A3%BE

4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?