Tips for adding a CSS class to an element while excluding any nested or child elements with the same style

Hey, I'm having some trouble applying a style to a body element without affecting a specific nested element selected by a certain selector. I attempted to use the :not pseudo-class but it doesn't seem to work for child elements. Here's an example of what I tried:

<!DOCTYPE html>
<html>
<head>
<style>
body:not(.not-disabled) {
  color: #ff0000;
  opacity: 0.4;
  pointer-events: none;
}
</style>
</head>
<body>

  <h1>This is a heading</h1>

  <p class="not-disabled">This is a paragraph.</p>
  <p class="not-disabled">This is another paragraph.</p>
  <button class="not-disabled">Enabled</button>
  <button>Disabled</button>
  <div>This is some text in a div element.</div>

</body>
</html>

I expected the paragraphs not to be transparent. Also, even though both buttons are disabled, I wanted one of them to be enabled, specifically: Enabled, but my expectations were incorrect.

Is there a way to achieve this? Please go easy on me as I'm new to CSS and despite searching online, I couldn't find a solution. It's possible that as a beginner, my questions are misguided.

Answer №1

body:not(.not-red) refers to styling a body element that does not have the class .not-red.

If you want to apply this style to the children of the body, simply add a space to the selector: body :not(.not-red)

body :not(.not-red) {
/*  ^ remember to include a space here */
  color: #ff0000;
}
<h1>This is a heading</h1>

<p class="not-red">This is a paragraph.</p>
<p class="not-red">This is another paragraph.</p>

<div>This is some text in a div element.</div>

Alternatively, you can override the style by placing the new style below the original one, as stylesheets are read from top to bottom.

body {
  color: #ff0000;
}
.not-red {
  color: black;
}
<h1>This is a heading</h1>

<p class="not-red">This is a paragraph.</p>
<p class="not-red">This is another paragraph.</p>

<div>This is some text in a div element.</div>

Answer №2

To change the color of elements, simply add the desired color to your not-red class

body {
  color: #ff0000;
}

.not-red {
  color: #000000;
}

CSS styles follow a cascading pattern, allowing you to set a default color in the body element and then override it with a different color for specific elements if needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to use a base64 encoded animated gif as a background in CSS

I embedded an animated gif into my CSS code like this: .magicBg { background-image: url(data:image/gif;base64,R0lGODlhAQBkAPcAAAAAAAEBAQICAgMDAwQEBAUFBQYGBgcHBwgICAkJCQoKCgsLCwwMDA0ND ... } This animated gif is set to play only once. However, after the ...

Pattern to locate a CSS class identifier

I've been working on creating a regex to match valid CSS class name structures. Currently, my progress looks like this: $pattern = "([A-Za-z]*\.[A-Za-z]+\s*{)"; $regex = preg_match_all($pattern, $html, $matches); However, there are certai ...

What could be causing the malfunction of my Superfish menu in Firefox?

I am currently experimenting with the Superfish jQuery plugin to improve a drop-down menu on my website. Unfortunately, in Firefox browser (v. 21.0), the drop-down menu does not open when hovering over it as expected. However, it works fine in Chrome and O ...

Ways to display an icon with an underline effect upon hovering over text

I have implemented a CSS effect where hovering over text creates an underline that expands and contracts in size. However, I now want to display an icon alongside the text that appears and disappears along with the underline effect. When I try using displa ...

Eliminate the display of Last Modified date and file Size in mod_autoindex

Recently, while developing a file storage webpage for my website, I successfully implemented mod_autoindex for my Apache server. However, I now face the challenge of removing the Last Modified and Size fields from the table. I am hopeful that there is a w ...

Tips for retrieving data from an Excel spreadsheet on an HTML/CSS webpage

I have a single HTML template at this location: . The current page is tailored for the state of Arkansas in the US, but I now need to replicate the design for all 50 states. Each state page will have the same layout, but different content specific to that ...

Embracing the Power of Sass

I have recently revamped my portfolio website on Github Pages and now I am looking to integrate Sass for my upcoming Portfolio 2.0 project. Although I have worked with Sass before, setting it up from scratch is new to me. Currently, I have installed Sass ...

Is it possible to modify the CSS of a parent element in vue.js only for the current router route?

I am trying to modify the parent component's style without affecting the CSS of other components. Here is an example from my code: App.vue <div class="page-content"> <router-view ref="routeview"></router-view> </div> rou ...

A technique for horizontally centering an image while simultaneously ensuring that the bottom is pushed down, even at an unknown resolution, and maintaining center alignment if

Imagine a scenario where I have an image with unknown resolution. My goal is to horizontally center it, even if the window's width is smaller than the picture, while also adjusting the bottom of the window to match the height of this picture. Is ther ...

What methods can be used to prevent divs from overlapping when the window size is reduced?

Creating a login page with two main content divs in a section layout. The first div contains the logo, input prompts, and login/password reset buttons. The second div serves as a footer with social media links. On a full-size window, the logo is positioned ...

Using Javascript to generate a button that randomly chooses links within the webpage

Looking for help with JavaScript to select a link when a button is clicked on the page? Check out my code and let me know what I'm doing wrong. For the full code, visit: here HTML <!doctype html> <html lang="it" xmlns="http:/ ...

Tips for incorporating background color with CSS pseudo-elements

I'm attempting to replicate the outcome shown in the screenshot but I'm having difficulty achieving it without adding any new DOM elements. When I click on the demo link and choose a date range, the start date and end date selections are not dis ...

Issues with jQuery compatibility when used alongside HTML and CSS

My coding tool is flagging errors in my JavaScript file, including: -'$' usage before definition. -Incorrect spacing between 'function' and '(' This is the content of my JS file: $(document).ready(function() { $(window).s ...

When a table cell is hovered over, a left border will be added

I've been working on adding a left border to the text in a way that highlights the first cell's border when hovering over a row, providing a clear indication of the current row for users. Feel free to check out my progress so far on this fiddle: ...

Requesting an image from the live website using a GET method

Recently, I registered a domain for my personal website. While the site is still a work in progress, I noticed that the logo image fails to display when viewed through the browser on the site. Surprisingly, it shows up perfectly fine when accessed via loca ...

Resize the images and align them side by side

I have a container with a maximum width of 1400px. Inside this container, there are two images of different sizes. I want to align them in a row using flexbox so that they fit within the 1400px width and still maintain a visually appealing appearance as sh ...

What is the best way to remove this H1 tag?

I am currently working on restructuring the HTML layout of my long-standing Wordpress website in order to make it more intuitive. However, I am faced with a challenge when it comes to understanding how the original designer set it up years ago. It seems th ...

Is there a way to modify or update the CSS classes and overall styling in .gsp files within the Grails framework?

What is the best way to dynamically update a DOM element's class and styling in response to specific conditions or events occurring in a .gsp file? ...

Looking for a way to scroll images without relying on the marquee tag? Whether you prefer using JavaScript, jQuery,

<marquee> <div class="client"> <img src="images/c1.png"/> </div> <div class="client"> <img src="images/c2.png"/> </div> <div class="client"> ...

How can you create an accordion menu that expands when a button is clicked?

Is there a way to make an accordion menu open when the 'More' button is clicked? I've tried, but it always starts in its expanded state and collapses when the button is clicked. What I really want is for the accordion to be closed initially ...