0
0

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 5 years have passed since last update.

css z-indexとtranslateY を同時に使うとうまくいかない。

Posted at

一緒に使わない書き方をすれば、この問題は一発で解決するけど、
原因がわからずに少し詰まったのでメモしておく。

サンプルコード

html
<ul>
    <li>text1<span>remove</span></li>
    <li>text2<span>remove</span></li>
</ul>

scss(css)です。

css
ul {
    li {
        transform: translateY(30px);
        width: 100%;
        position: relative;
        span {
            position: absolute;
            width: 50px;
            height: 20px;
            top: 100%;
            left: 0;
            z-index: -1;
            opacity: 0;
            background: red;
        }
        &:hover span {
                z-index: 1;
                opacity:1;
            }
    }
}

やりたかったこと。

liをhover したときにspan要素を表示させる。
上記コードの場合うまくいかない。

translateY を削除すると期待通りの動きになった。

上記cssコードからtransform: translateY(30px);を削除したら、期待している動きになった。
削除というか、translateY使いたい場合、ulにtransformを当てれば良い。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?