CSS performance will also affect the number of page requests loading speed and rendering efficiency, summarizes the performance optimization of CSS mainly in the following aspects:
One, the CSS file size compression, mainly to the number of bytes compression, CSS file with size will increase the network overhead, especially the larger traffic volume of the page. The main methods are as follows:
1. Reduce unnecessary extra characters such as CSS file Hollow lattice, and use as many abbreviations as possible in grammar (such as margin 10px;).
2. A bit of browser support decompression compressed files in gzip format, you can set the server to compress the corresponding files into gzip package.
Second, the CSS structure of the extraction, split, if necessary, can be loaded as needed to load CSS files, as much as possible to use the browser cache
Extraction is mainly out of the common properties, the attributes of the attribute split out, each page loaded with common attributes of the file, only the need for the page or need to load the feature file, this can avoid the structure of the CSS file large, logical confusion. However, sometimes the split will result in the increase in the number of page requests, the overall page needs to consider.
Third, the CSS syntax is mainly concerned about the efficiency of the selector:
The fundamental principle of the analysis selector is that the resolution of the selector is resolved from right to left. So the right-most choice has a larger impact on performance, generally called a key selector. The more specific the key selector, the faster the speed:
Efficiency sequencing for common selectors:
ID selector > class selector > tag Selector (div H1) > adjacent selector (h1+p) > Child selector (ul>li) > descendant selector (li a) > Wildcard > Property selector (A[rel = "Ex Ternal "]) > Pseudo class selector (a:hover)
General rules:
1. Before the ID selector avoids the parent selector, the ID selector has been able to navigate to the exact label without having to filter the parent class, if necessary add the parent class stating that the page ID does not follow uniqueness.
2. Avoid the parent selector before the class selector, and if you must precede the class selector with a parent selector to explain the structure of the class selector, it is a good idea to refactor the CSS.
3. Avoid using descendant selectors as much as possible. The descendant selector will always traverse all parent nodes until the root node is found, so the performance consumption is greater
4. The key selector of the sub-selector should be as specific as possible.
The CSS file location is best placed in the head, that is, in the page structure parsing, you can make page rendering. If you put it in the body or after the body, it will appear if the page is relatively large load slower, then the user will see the non-rendered page at the beginning, until the page is finished loading before the rendering begins.
CSS Performance Optimizations