How to improve css performance
This article mainly introduces the methods of CSS performance optimization to improve CSS performance. Non-standard CSS will cause many performance problems, so it is very necessary to learn and master CSS performance optimization skills. Friends who are interested in CSS performance optimization knowledge come together Let’s learn
Irregular CSS will cause a lot of performance problems. These problems may not be obvious enough in some small projects, but they will become apparent in large projects.
css matching principle
Before optimizing css, we need to understand how css works. We all know that css is composed of selectors , consisting of attributes and attribute values.
We may write a line of code like this
//css .con .loulan1 p span{ display: block; } //html <p class="con"> <p class="loulan"> <p><span>文字</span></p> </p> </p>
Here we define the style of the span tag in the p tag in the loulan class in the con class. I'm tired of saying it, let alone writing it. In fact, you can think of the browser as a person, and it will definitely waste performance when rendering.
And the matching principle of css is not from left to right, but from right to left. That is to say, in our line of code, we first find all the span elements in the page to form a set, and then Search up all the span elements to see how many spans have a parent element that is a p element or a parent element whose parent element is a p element or... Search slowly and delete the ones whose parent element is not a p element. Go up and search to see how many p elements there are in the collection. The class of its parent element is loulan... The browser said: I am so tired...
In fact, the browser searches from right to left. The advantage is to filter out some irrelevant style rules and elements as early as possible. And Firefox calls this search method keyselector (keyword query). The so-called keyword is the last (rightmost) rule in the style rules. The key above is span. The original purpose is to filter out some irrelevant style rules as quickly as possible. What we lack here is layer upon layer, layer after layer without stopping. So if I just want to define the style of a span, wouldn't it be better to add a class to the span? Someone wants to say at this time, is it necessary to add a class to every element? That is definitely not necessary, but we need to understand how the browser finds matches, and then combine it with the current structure to make the best and most convenient way of writing.
//css .loulanSpan{ display: block; } //html <p class="con"> <p class="loulan"> <p><span class="loulanSpan">文字</span></p> </p> </p>
css selector weight
Here are the weights of the CSS ID Class Tag selector, which is their priority. , the greater the weight, the higher the priority
ID:100
Class:10
Tag:1
With the foundation of the above two Let’s talk in detail about how to optimize css to improve performance
1. Reduce css nesting. It is best not to nest more than three layers. Generally, block-level elements are added with classes, and inline elements inside are not added. , when writing css, use block-level classes to set inline tags. This can not only reduce the size of the css file, but also reduce performance waste.
2. Do not nest in front of the ID selector. ID is inherently unique and the weight is so large. Nesting in front is a complete waste of performance.
3. Create a public style class and extract the same style for a long section and use it as a public class. For example, we commonly use clear floats, display ellipsis when a single line exceeds the limit, etc. Of course, if you use sass, inheritance will let you It is more convenient. At the same time, I advocate the use of sass. I will definitely write a sass blog in the future.
4, abbreviation css, including abbreviation maigin, padding, color value, etc., if there are two or more margin-****, write margin: * * * *Help file size.
5, reduce the use of wildcard * or selectors like [hidden="true"], search all one by one... Is this performance good? Of course, necessary things like resetting the style are indispensable.
6. Some people like to add the tag name: p.ty_p in front of the class name for more precise positioning, but this is often less efficient. The class name should be in the global scope unless the public is unique, so This approach should be done instantaneously.
7, cleverly use the inheritance mechanism of CSS. Many attributes in CSS can be inherited, such as color, font, etc. Once the parent node is defined, the child nodes do not need to be defined.
8. Split the public css file. For larger projects, we can extract the common structure styles of most pages and put them into separate css files, so that they can be placed in the cache after one download. Of course, this approach will increase requests, and the specific approach should be determined based on the actual situation.
9. There is no need for css expressions. You may have less exposure to them, but what you need to remember is that no matter how cool we are, it will be static in the end, so expressions just make your code look cooler. , but its waste of performance may be beyond your imagination, because it is not just calculated once, some small events may increase the number of calculations it needs to perform in order to be effective and accurate.
10. Use less css rest. You may think that resetting the style is the norm, but in fact many of the operations are unnecessary and unfriendly. Friends who have needs and interests can choose normolize.css
11, cssSprite, synthesizes all icon images, and uses the width, height and bacgroud-position background image to display the icon image we want. This is a very practical technique that greatly reduces http requests.
Of course we still need some aftermath work, CSS compression (here is an online compression YUI Compressor, of course you will use other tools to compress it is very good), GZIP compression, Gzip is a popular file compression algorithm, details You can search it on Google or Baidu.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
The above is the detailed content of How to improve css performance. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
