This time I will show you how to use CSS selectors efficiently, and what are the precautions for using CSS selectors efficiently. The following is a practical case, let’s take a look.
When I first wrote CSS, I thought it was very simple and could be written as wildly as possible. Later, I gradually discovered that although writing without rules can achieve results, in actual development, it may result in more useless or repeated work, and CSS is not efficient. Therefore, during later development, we deliberately followed certain rules to write efficient and reusable CSS as much as possible. To summarize, the main aspects are the following. First look at a short piece of CSS code:#menus > li { font-size: 14px; }


1. Avoid using wildcard rules
In addition to wildcard selectors in the traditional sense, we have classified adjacent sibling selectors, sub-selectors, and descendant selectors that match attribute selectors into wildcard rule categories. It is recommended to only use ID, class, and label selectors. .2. Do not limit the ID selector
A specified ID can only correspond to one element on the page, so there is no need to add additionalqualifiers. For example, p#header is unnecessary and should be simplified to #header.
3. Do not limit the class selector
Do not limit the class selector with specific labels, but expand the class name according to the actual situation. For example, change li.chapter to .li-chapter, or even better, .list-chapter.4. Make the rules as specific as possible
Don’t try to write long selectors like ol li a, it’s better to create one like .list-anchor class and add it to the appropriate element.5. Avoid using descendant selectors
Usually the cost of processing descendant selectors is the highest, but using sub-selectors can also get the desired results, and More efficient.6. Avoid using tag-sub-selectors
If there are tag-based sub-selectors like #menus > li > a, then they should be used A class to associate with each label element, such as .menus-item.7. Question all uses of subselectors
Examine all uses of subselectors and replace them with concrete classes whenever possible.8. Rely on inheritance
Understand which attributes can be inherited, and then avoid repeatedly specifying rules for these attributes. For example, specify I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:CSS3 to create a striped background
css3 to create a semicircular arc
The above is the detailed content of How to use CSS selectors efficiently. For more information, please follow other related articles on the PHP Chinese website!
How much specificity do @rules have, like @keyframes and @media?Apr 18, 2025 am 11:34 AMI got this question the other day. My first thought is: weird question! Specificity is about selectors, and at-rules are not selectors, so... irrelevant?
Can you nest @media and @support queries?Apr 18, 2025 am 11:32 AMYes, you can, and it doesn't really matter in what order. A CSS preprocessor is not required. It works in regular CSS.
Quick Gulp Cache BustingApr 18, 2025 am 11:23 AMYou should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser
In Search of a Stack That Monitors the Quality and Complexity of CSSApr 18, 2025 am 11:22 AMMany developers write about how to maintain a CSS codebase, yet not a lot of them write about how they measure the quality of that codebase. Sure, we have
Datalist is for suggesting values without enforcing valuesApr 18, 2025 am 11:08 AMHave you ever had a form that needed to accept a short, arbitrary bit of text? Like a name or whatever. That's exactly what is for. There are lots of
Front Conference in ZürichApr 18, 2025 am 11:03 AMI'm so excited to be heading to Zürich, Switzerland for Front Conference (Love that name and URL!). I've never been to Switzerland before, so I'm excited
Building a Full-Stack Serverless Application with Cloudflare WorkersApr 18, 2025 am 10:58 AMOne of my favorite developments in software development has been the advent of serverless. As a developer who has a tendency to get bogged down in the details
Creating Dynamic Routes in a Nuxt ApplicationApr 18, 2025 am 10:53 AMIn this post, we’ll be using an ecommerce store demo I built and deployed to Netlify to show how we can make dynamic routes for incoming data. It’s a fairly


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools






