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?

もっとモダンにcssを書いてみよう(where, is, has)

Posted at

最近ではtailwindなどのcssフレームワークを使うことが多いのでcssを直書きする場面がどんどん減ってきています。

しかし、それでもUIライブラリーの中身を修正したりする時にはcssを書く必要が出てきますよね。
ということで、今回はもう少しモダンな書き方を3つ見ていきます!

:where()

:where()は複数のセレクタをまとめるための擬似クラスで、詳細度に影響を与えません。
詳細度は他のcssよりどれだけ優先されるかを表すものです。以下の記事に詳しく書いてました!

main h1, main h2, main h3 {
  color: #ffffff;
}
main :where(h1, h2, h3) {
  color: #ffffff;
}

:is()

:is()も複数のセレクタをまとめます。しかし、:where()と違ってグループ内の最も詳細度の高いセレクタの詳細度を継承します。もっと優先させることができると思っていただければ!

main h1, main h2, main h3 {
  color: #ffffff;
}
main :is(h1, h2, h3) {
  color: #ffffff;
}

:has()

:has()は「〜を持つ要素」を選択する親セレクタです。JavaScriptでの判定がなくても子要素の状態に基づいて親要素を選択できます。

.parent.checked {
  background: lightgreen;
}
.parent:has(input:checked) {
  background: lightgreen;
}

おわりに

こういう豆知識的なものが集まって生産性がどんどん上がっていくのでもっと勉強していきたいですね。

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?