- Mastering CSS
- Rich Finelli
- 225字
- 2021-07-08 09:45:54
Calculating the weight of selectors
A class is worth 10 points, so .secondary-section is worth 10 points. A plain old element such as p or div is worth 1 point. Therefore, the .secondary-section p selector is 11 points. The .secondary-section div figure img selector is 13 points. Let's create another selector below the one worth 13 points and we have .secondary-section img. Then, let's change the border-color to blue:
.secondary-section div figure img { border: 10px solid #333; }
.secondary-section img {
border: 10px solid blue;
}
When we save this, our border is going to remain gray because the point value of our last selector is only 11; it's getting beat out by the previous selector's point value of 13. That's the problem with these long descendant selectors that are longer than they should be, they get weighted heavier:

Following is the output of preceding code:

IDs have a point value of 100 points, which is why I advise against using them. They have so much unnecessary weight to them and drive the specificity level through the roof. Assigning point values kind of sounds like keeping score on a video game, but the difference is you want to try and keep your point value as low as possible on this game. If you do that, you will be able to write less complicated CSS.
- Spring Cloud Alibaba微服務架構設計與開發實戰
- Blockly創意趣味編程
- The DevOps 2.4 Toolkit
- Linux命令行與shell腳本編程大全(第4版)
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- Getting Started with Laravel 4
- Java實戰(第2版)
- 利用Python進行數據分析
- Python深度學習:模型、方法與實現
- Multithreading in C# 5.0 Cookbook
- Android驅動開發權威指南
- 深度實踐KVM:核心技術、管理運維、性能優化與項目實施
- Python網絡爬蟲實例教程(視頻講解版)
- 少年小魚的魔法之旅:神奇的Python
- AngularJS by Example