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.

:has()疑似クラスの使い方メモ

Last updated at Posted at 2022-10-17

今後のデザインで採用するかもしれない疑似クラスのメモ書き
cssで意図したスタイル、アニメーションを当てられないときはJavaScriptで実装する。もし、:has()疑似クラスを使えばJavaScriptを使用しなくても、指定したセレクタやDOMを取得し、任意のスタイルを当てることが出来るようになる。
使い方の一例をあげる

<div class="card">
    <div class="card-image">
        <img src="awameh.jpg" alt="">
    </div>
    <div class="card-content">
    </div>
</div>
 
<div class="card">
    <div class="card-content">
    </div>
</div>

.card:has(.card__image) {
    display: flex;
    align-items: center;
}

上のコードでは子要素に.card__imageがある.cardにCSSが適応されます。

現時点ではFirefoxはまだサポートされていない。
has():疑似クラスのサポート状況
なので、まだ採用はしない、JavaScriptや、CSSのの記述量を減らせるのは大きいので早くサポートしてほしい(´・ω・`)

参考:

CSSの:has()疑似クラスの便利な使い方を徹底解説

0
0
1

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?