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

200513学習記録:CSSの優先順位

Last updated at Posted at 2020-05-13

TechAcadmy lesson3 9.CSS

CSSの優先順位で覚えておくこと2つ
①下に書かれたものが優先
②詳細なセレクタが優先


①下に書かれたものが優先
→これは「green」が表示される。

p {
    color: red;
}
p {
    color: green;
}

②詳細なセレクタが優先
→これは「red」が表示される(もちろんclass指定されているpタグのところで)。

p.important {
    color: red;
}
p {
    color: green;
}

もうちょっと詳しく言うと
(CSS3スタンダード・デザインガイド参照)

セレクタごとに↓のa~dの値を算出し、aから順に比較して値が大きい場合は優先順位が高くなる仕組みになっている。

a インラインスタイルシートに記述
b IDの数
c クラス/疑似クラスの数
d 要素名/疑似要素の数

# sand{
 background-color:yellow;
 }

div h1{
 background-color:red;
 }

h1{
  background-color:pink;
 }

↓値を計算すると

# sand → bが1
div h1 → dが2
h1 → dが1

なので優先準備が最も高いのが#sand

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?