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 1 year has passed since last update.

CSSで特定の要素の兄弟要素を拾う

Posted at

初めに

リストの要素をクリックするとis-activeクラスが付与されるが、それ以外は変わらない。
その状態で「is-activeがない状態」「is-activeがある状態のis-active」「is-activeがある状態のis-active以外」の要素デザインを切り替えねばならなかった。

解決策

CSSで以下のようになった。

ul{
    li{
        background: #FFFFFF;
        &:has( ~ .is-active),
        &.is-active ~ li{
          background: #F6F6F6;
        }
        &.is-active{
          background: #CCCCFF;
        }
    }
}

hasセレクタで「以下に兄弟が.is-active」と、「.is-active移行の兄弟要素」を選択することで
.is-active以前と以降を両方変えることが出来た。

参考

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?