laitimes

Front-end performance optimization cheats: master the correct posture of CSS selectors

author:Flash Gene

In today's Internet era, web performance optimization has become an indispensable part of front-end development. Whether it's from a user experience standpoint, or from a search engine optimization (SEO) standpoint, web page load speed is crucial. In this fast-paced world, users expect seamless and smooth web browsing, and for slow or unresponsive websites, they often choose to leave and look for other, better alternatives.

In the process of optimizing the loading speed of web pages, the role of CSS selectors cannot be ignored. As one of the key tools to define the style of web pages, CSS selectors directly affect the rendering performance of the browser, determining the page loading speed and the quality of the user experience. A website that uses CSS selectors reasonably and efficiently can not only improve page loading speed and reduce user waiting time, but also improve user satisfaction and enhance user stickiness, which in turn can bring more traffic and revenue.

In this article, we'll take a deep dive into how you can improve front-end performance through the proper and efficient use of CSS selectors, making web pages load faster and user interactions smoother. We will explore the different types of CSS selectors and how to use them, and introduce some tips for optimizing CSS selectors to help developers better understand and apply CSS selectors to improve the performance of web pages and improve the user experience.

What sets good developers apart is their ability to carefully consider the performance impact when coding.

1. The role and type of CSS selector

CSS selectors are used to select elements in an HTML or XML document and apply styles to those elements. Selectors allow you to specify specific elements to style, allowing you to style and layout your page. The selector allows developers to select elements based on their tag name, class name, ID, attributes, position relationship, and other conditions, and then define style rules for these elements, such as color, font, size, spacing, etc., to achieve the appearance and layout of the page. CSS selectors are an important part of CSS stylesheets, and they allow developers to control the appearance and performance of page elements in a targeted manner, resulting in more flexible and aesthetically pleasing web designs.

Let's briefly review the common types of CSS selectors and how to use them:

Element Selector: Select elements based on HTML tags, for example:

p {
   color: red;
}           

This selects all <p>the tabs.

Front-end performance optimization cheats: master the correct posture of CSS selectors

Class Selector: Selects elements based on their 'class' attributes, denoted by a dot ('.'), for example:

.highlight {
background-color: yellow;
}           

This selects all elements that have 'class="highlight"'

Front-end performance optimization cheats: master the correct posture of CSS selectors

ID Selector: Selects elements based on their 'id' property, denoted by a period ('#'), for example:

#header {
font-size: 24px;
}           

This selects the element with 'id="header"'.

Front-end performance optimization cheats: master the correct posture of CSS selectors

Descendant Selector: Based on all elements that are descendants of an element, for example:

.container div {
border: 1px solid black;
}           

This selects the element inside all elements of the .container class<div>.

Front-end performance optimization cheats: master the correct posture of CSS selectors

Child Element Selector: Selects all elements that are direct children of an element, denoted by a greater than sign ('>'), for example:

ul > li {
padding-left: 20px;
}           

This selects all <ul>elements that are directly inside the element<li>.

Front-end performance optimization cheats: master the correct posture of CSS selectors

Brother selector

1. Neighbor sibling selector: Indicated by a plus sign ('+'), for example:

h2 + p {
margin-top: 0;
}           

This selects <h2>the element immediately following the element<p>.

Front-end performance optimization cheats: master the correct posture of CSS selectors

2. Universal Brother Selector: Indicated by a dash ('~'), for example:

h2 ~ p {
color: red;
}           

This <h2>selects the element after all the elements<p>.

Front-end performance optimization cheats: master the correct posture of CSS selectors

Attribute Selector: Selects elements based on their properties and their values, for example:

p[type="test"] {
color: red
}           

This selects all elements of type 'test'.<p>

Front-end performance optimization cheats: master the correct posture of CSS selectors

Pseudo-class selector: Used to add styles to elements in a specific state, such as:

1. ':hover':鼠标悬停时

a:hover {
color: red;
}           
Front-end performance optimization cheats: master the correct posture of CSS selectors

2. :focus: When an element gains focus

input:focus {
background-color: yellow;
}           
Front-end performance optimization cheats: master the correct posture of CSS selectors

3. :first-child: When an element is the first child of its parent

li:fist-child{
background-color: red;
}           
Front-end performance optimization cheats: master the correct posture of CSS selectors

Pseudo-Element Selector: Used to add special effects to specific parts of an element, such as:

1. '::before': Adds content in front of the element's content

p::before {
content: "→";
}           
Front-end performance optimization cheats: master the correct posture of CSS selectors

2. '::after': Adds content after the element's content

p::after {
content: "⏎";
}           
Front-end performance optimization cheats: master the correct posture of CSS selectors

Wildcard selector: Using an asterisk ('*') can match any element, but should be used with caution as it can degrade performance, for example:

* {
color: pink;
}           

This selects all DOM elements.

Front-end performance optimization cheats: master the correct posture of CSS selectors

Descendant pseudo-class :not() selector: Used to exclude elements that are matched by a simple selector, for example:

p:not(.highlight) {
font-size: 18px;
}           

This selects all elements that don't have a highlight class<div>.

Front-end performance optimization cheats: master the correct posture of CSS selectors

Pseudo-class :nth-child() selector: Used to select specific child elements that belong to their parents, for example:

li:nth-child(even) {
background-color: lightgray;
}           

🔥 This selects all even-numbered <li>elements. odd stands for odd number.

Front-end performance optimization cheats: master the correct posture of CSS selectors

后代伪类`:nth-of-type()`选择器,类似于`:nth-child()`,但它只选择特定类型的元素,例如:

p:nth-of-type(2) {
font-weight: bold;
}           

This <p>selects the second instance of each element.

Front-end performance optimization cheats: master the correct posture of CSS selectors

CSS selector list: When applying the same style to multiple selectors, you can separate them with a comma, for example:

h1, h2, h3 {
color: green;
}           

This selects all the <h1>''', '<h2>', and '<h3>' elements.

Front-end performance optimization cheats: master the correct posture of CSS selectors

These selectors can be used individually or in combination to meet a variety of styling and layout needs.

By using CSS selectors wisely and efficiently, we can improve front-end performance, make web pages load faster, and user interactions smoother. When writing CSS styles, you should pay attention to the weight and performance impact of selectors, avoid unnecessary selectors, simplify the selector structure, and try to use efficient selector types to improve the rendering performance of the page.

These optimization strategies not only improve web page performance, but also help improve the maintainability and readability of the code, making front-end development work more efficient and enjoyable.

Author: Home-Zhao Ying

Source-WeChat public account: Home front-end sharing stream

Source: https://mp.weixin.qq.com/s/A8nGbxGzEOMBc8gfTyJhEw