Navigate back to the homepage

CSS 選擇器的優先權順序

CSS
November 17th, 2021 · 1 min read
  1. 在相同選擇器下,後者敘述優先於前者。

  2. 在不同選取器下, 權重依輕到重:

  • Tag 選取器 (如: spanh1p)、 伪元素 (如:::before::after

  • Class 選取器 (如: .container.wrap) 、 偽類(如::hover:focus)、 属性选择器 (如: [type="radio"] [attr=value]

  • Id 選取器 (如: #wrap)

  • Inline style (如: style="color:red")

  • !important (如: color: red !important;)


結論:

!important > Inline style > Id 選取器 > Class 選取器、偽類、属性选择器 > Tag 選取器、伪元素

參考資料

https://developer.mozilla.org/zh-CN/docs/Web/CSS/Specificity

More articles from Iris Blog

DOCTYPE 是什麼

在 HTML 文件一開始第一行我們都要寫上 DOCTYPE

November 16th, 2021 · 1 min read

Day13: Slide in on Scroll

滾動視窗到定點時動畫滑入圖片的效果。

July 7th, 2021 · 1 min read
© 2020–2021 Iris Blog
Link to $mailto:qweichew@gmail.comLink to $https://github.com/iris1114Link to $https://www.instagram.com/iris.justdoit/Link to $https://www.linkedin.com/in/iris-chew