CSS中如何使用!important聲明強制優先的具體介紹

來源:互聯網
上載者:User
!important聲明強制優先

CSS優先順序中還有一個最無敵的聲明,就是!important。

在CSS樣式表中,帶有!important聲明的樣式優先使用,它的優先順序會超越任何地方、任何方式的樣式聲明,包括行內style屬性中聲明的樣式。

比如下面代碼:

<style type="text/css"> p{ color:red !important; }  </style> <p style="color: blue; ">這是一行文字</p>

這裡的這行文字在瀏覽器中仍然顯示為紅色字型。

所以,CSS樣式匹配的優先權遵循以下順序:

(1) !important聲明高於一切。

(2) 內嵌樣式次之。

(3) 樣式表中聲明按選取器的權重排序。

(4) 最後按瀏覽器執行樣式表的順序,遵循"後來居上"原則匹配樣式。

當然最後還要強調一點:如果沒有設定樣式的,則自動繼承父級節點的樣式。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.