search
HomeWeb Front-endCSS TutorialHow to Exclude Specific Class Names in CSS Selectors?

How to Exclude Specific Class Names in CSS Selectors?

Excluding Specific Class Names in CSS Selectors

In CSS, there are instances where you may need to exclude particular class names from your selectors. This is especially useful when you want to apply styles to multiple elements, but certain ones should not inherit those styles.

One common scenario involves excluding elements with a specific class name while applying styles to elements with another class name. Let's consider the following example:

<code class="html"><a href="" title="Design" class="reMode_design reMode_hover">
    <span>Design</span>
</a>

<a href="" title="Design" class="reMode_design reMode_hover reMode_selected">
    <span>Design</span>
</a></code>

In this example, we want to apply a background color to elements with the "reMode_hover" class name on hover. However, we do not want to apply this color if the element also has the "reMode_selected" class name.

<code class="css">/* Do not apply background-color (leave empty) */
.reMode_selected .reMode_hover:hover { }

.reMode_hover:hover { background-color: #f0ac00; }</code>

While you might expect the first rule to work, it does not. This is because in CSS, space between class names denotes the descendant selector. So, ".reMode_selected .reMode_hover" translates to "select .reMode_hover elements that are descendants of .reMode_selected."

To exclude "reMode_selected" elements correctly, we need to use the "not()" selector. The updated CSS would be:

<code class="css">.reMode_hover:not(.reMode_selected):hover { background-color: #f0ac00; }</code>

This rule will apply the background color to .reMode_hover elements that do not have the .reMode_selected class name. As a result, only the first link will receive the background color on hover, while the second link will not.

The above is the detailed content of How to Exclude Specific Class Names in CSS Selectors?. 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
Demonstrating Reusable React Components in a FormDemonstrating Reusable React Components in a FormApr 16, 2025 am 10:36 AM

Components are the building blocks of React applications. It’s almost impossible to build a React application and not make use of components. It’s widespread

Filtering Data Client-Side: Comparing CSS, jQuery, and ReactFiltering Data Client-Side: Comparing CSS, jQuery, and ReactApr 16, 2025 am 10:35 AM

Say you have a list of 100 names:

Zero hands up.Zero hands up.Apr 16, 2025 am 10:34 AM

Asked an entire room full of webdevs yesterday if any of them knew that FF/Chrome/Opera/Brave/etc. for iOS weren't allowed to compete on engine quality.

Variable Fonts Link Dump!Variable Fonts Link Dump!Apr 16, 2025 am 10:22 AM

There's been a ton of great stuff flying around about variable fonts lately (our tag has loads of stuff as well). I thought I'd round up all the new stuff I

Link Underlines That Animate Into Block BackgroundsLink Underlines That Animate Into Block BackgroundsApr 16, 2025 am 10:14 AM

It's a cool little effect. The default link style has an underline (which is a good idea) and then on :hover you see the underline essentially thicken up

Preloading Pages Just Before They are NeededPreloading Pages Just Before They are NeededApr 16, 2025 am 09:53 AM

The typical journey for a person browsing a website: view a page, click a link, browser loads new page. That's assuming no funny business like a Single Page

Adaptive Photo Layout with FlexboxAdaptive Photo Layout with FlexboxApr 16, 2025 am 09:51 AM

Let’s take a look at a super lightweight way to create a horizontal masonry effect for a set of arbitrarily-sized photos. Throw any set of photos at it, and

The Many Ways to Link Up Shapes and Images with HTML and CSSThe Many Ways to Link Up Shapes and Images with HTML and CSSApr 16, 2025 am 09:45 AM

Different website designs often call for a shape other than a square or rectangle to respond to a click event. Perhaps your site has some kind of tilted or

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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),