search
HomeWeb Front-endFront-end Q&AHow to use selector to exclude elements in css

CSS is one of the important skills in front-end development. In addition to the basic selection of elements and modification of styles, selection and exclusion is also a very important knowledge point in CSS. This article will explain selection exclusions in CSS and provide practical cases for reference.

Introduction to selectors

Before learning to select and exclude, let’s first understand the basics of CSS selectors.

CSS selector is used to select HTML elements. In CSS, there are many kinds of selectors, such as basic selectors, hierarchical selectors, pseudo-class selectors, etc. These selectors have different uses and syntax.

The following are several types of basic selectors:

  • Tag selector: Select by the tag name of the HTML element, for example: p{color:red}
  • Class selector: Select through the class attribute of the HTML element, for example: .myclass{color:blue}
  • ID selector: Select through the class attribute of the HTML element ID attribute to select, for example: #myid{color:green}
  • Wildcard selector: select all HTML elements, for example: *{color:pink}
  • Attribute selector: Select through the attribute value of the HTML element, for example: [href="http://www.example.com"]{color:purple}

Through the combination of the above basic selectors, we can accurately select HTML elements and modify their styles through CSS.

Select to exclude

Sometimes we not only need to select elements, but also need to exclude some elements. At this time, selecting to exclude is very useful.

There are several ways to exclude selection in CSS:

  • :not pseudo-class
  • :first-child pseudo-class
  • :last -child pseudo-class
  • :only-child pseudo-class

Let’s introduce them one by one.

:not pseudo-class

:not pseudo-class is used to exclude certain elements. Through simple syntax, we can exclude specified elements in the selector.

For example, we want to select all p elements, but exclude elements with class="no" in the p elements, You can use the following syntax:

p:not(.no){color:blue}

This way you can accurately select all p elements, but exclude elements with class="no" and replace them The text font color is changed to blue.

:first-child pseudo-class

:first-child pseudo-class indicates selecting the first child element, and can also be used to exclude certain elements.

For example, we want to select the first child element a of all li elements, but exclude elements with class="no" Elements, you can use the following syntax:

li > a:first-child:not(.no){color:blue}

This way you can accurately select the first child element a of all li elements, but exclude the elements with class="no" elements, change their text font color to blue.

:last-child pseudo-class

:last-child pseudo-class indicates selecting the last child element, and can also be used to exclude certain elements.

For example, we want to select the last child element li of all ul elements, but exclude those with class="no" element, you can use the following syntax:

ul > li:last-child:not(.no){color:blue}

This way you can accurately select the last child element li of all ul elements, but exclude elements with For elements with class="no", change their text font color to blue.

:only-child pseudo-class

:only-child pseudo-class indicates selecting the only child element, and can also be used to exclude certain elements.

For example, we want to select all div elements, but only the unique child element p, and exclude elements with class="no" elements, you can use the following syntax:

div > p:only-child:not(.no){color:blue}

This way you can accurately select the only child element p of all div elements, but exclude For elements with class="no", change their text font color to blue.

Actual Case

The following provides an actual case to show how to use selection exclusion.

Case description: There is a dynamically generated navigation bar menu, some of which have drop-down menus. Now it is necessary to change the font color of all menus to red, but exclude menus with drop-down menus and their drop-down menus. font color.

HTML code:



CSS code:

.menu li > a:not(.submenu > li > a){
  color: red;
}

Explanation: Through the :not() pseudo-class, we can use The li element of submenu and its child elements submenu > li are excluded, and only all li elements and child elements a are selected. And change its font color to red.

The above is the basic knowledge and practical application of selection exclusion. In actual development, selection exclusion is a very practical technique that can help us select HTML elements and modify their styles more efficiently.

The above is the detailed content of How to use selector to exclude elements in css. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is useEffect? How do you use it to perform side effects?What is useEffect? How do you use it to perform side effects?Mar 19, 2025 pm 03:58 PM

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Explain the concept of lazy loading.Explain the concept of lazy loading.Mar 13, 2025 pm 07:47 PM

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?What are higher-order functions in JavaScript, and how can they be used to write more concise and reusable code?Mar 18, 2025 pm 01:44 PM

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

How does currying work in JavaScript, and what are its benefits?How does currying work in JavaScript, and what are its benefits?Mar 18, 2025 pm 01:45 PM

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

How does the React reconciliation algorithm work?How does the React reconciliation algorithm work?Mar 18, 2025 pm 01:58 PM

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

How do you prevent default behavior in event handlers?How do you prevent default behavior in event handlers?Mar 19, 2025 pm 04:10 PM

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

What is useContext? How do you use it to share state between components?What is useContext? How do you use it to share state between components?Mar 19, 2025 pm 03:59 PM

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

What are the advantages and disadvantages of controlled and uncontrolled components?What are the advantages and disadvantages of controlled and uncontrolled components?Mar 19, 2025 pm 04:16 PM

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools