推薦:
.class
選擇器要高於標籤選擇器。#id
選擇器要高於.class
選擇器。!important
的屬性它的權重值優先順序最高的,大於所有的選擇器。讓我們進入標籤選擇器和.class
選擇器誰的優先順序高實踐,實踐內容如:將HTML
頁面中的h2
標籤設定文字顏色。
程式碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>優先順序</title> <style> h2{ color: red; /*紅色*/ } .box{ color: springgreen; /*綠色*/ } </style> </head> <body> <h2 class="box">微笑是最初的信仰</h2> </body> </html>
結果圖
讓我們進入.class
選擇器和id
選擇器誰的優先順序高實踐,實踐內容如:將HTML
頁面中的h2
標籤設定文字顏色。
程式碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>優先順序</title> <style> h2{ color: red; /*紅色*/ } .box{ color: springgreen; /*綠色*/ } #box{ color:blue; /*藍色*/ } </style> </head> <body> <h2 class="box" id="box">微笑是最初的信仰</h2> </body> </html>
結果圖
現在我們知道了標籤選擇器優先順序最低,那麼筆者將標籤選擇器新增!important
屬性呢,誰的優先順序更高呢?
!important權重使用格式如下:
color: red !important; /*紅色*/
注意:屬性:值 !important
屬性值用空格隔開即可。
讓我們進入 !important
屬性使用實踐,看看!important
屬性威力有多大哈。
程式碼塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>!important使用</title> <style> h2{ color: red !important; /*紅色*/ } .box{ color: springgreen; /*綠色*/ } #box{ color:blue; /*藍色*/ } </style> </head> <body> <h2 class="box" id="box">微笑是最初的信仰</h2> </body> </html>
結果圖
優先順序從低到高如:標籤選擇器、.class
選擇器、#id
選擇器、!important
屬性
更多程式設計相關知識,請存取:!!
以上就是了解CSS的選擇器優先順序和!important權重的詳細內容,更多請關注TW511.COM其它相關文章!