CSS code for "Ancestor Of"

Imagine I have this snippet of HTML

.A {
  border: thick blue solid;
}

.B {
  border: medium red solid;
}
<div class="root">
  <div class="A">
    <div class="B">
      DON'T SELECT ME
    </div>
  </div>

  <div class="B">
    SELECT ME
  </div>

  <div class="A">
    <div class="C">
      <div class="B">
        ALSO DON'T SELECT ME
      </div>
    </div>
  </div>
</div>

If I want to change the background color to yellow of the "SELECT ME" div only, how can I achieve that without affecting the other elements? I thought it could be done like this:

.root>:not(.A) .B { background: yellow; }

However, this doesn't seem to work and I am not sure why. My searches online haven't provided a solution either. Is it possible to accomplish this with CSS?

Answer №1

.X {
  border: thin purple dashed;
}

.Y {
  border: thick orange solid;
}

.parent > .Y {
  background-color: pink;
}

.X > .Y {
  background-color: blue;
}
<div class="parent">
  <div class="X">
    <div class="Y">
      AVOID SELECTING THIS
    </div>
  </div>

  <div class="Y">
    CHOOSE ME
  </div>

  <div class="X">
    <div class="Z">
      <div class="Y">
        ALSO AVOID THIS
      </div>
    </div>
  </div>
</div>

Targeting .Y as a direct descendant of .parent succeeds:

.parent>.Y { background: pink; }

Targeting .Y as a direct descendant of .X would enable different styling for the .X > .Y selectors:

.X>.Y { background: blue; }

Answer №2

If you want to style specific elements using CSS, consider utilizing the nth-child rule. You can find more details here

.B:nth-child(2) {
   background-color: yellow;
}

.A {
  border: thick blue solid;
}

.B {
  border: medium red solid;
}
.B:nth-child(2) {
   background-color: yellow;
}
<div class="root">
  <div class="A">
    <div class="B">
      DON'T SELECT ME
    </div>
  </div>

  <div class="B">
    SELECT ME
  </div>

  <div class="A">
    <div class="C">
      <div class="B">
        ALSO DON'T SELECT ME
      </div>
    </div>
  </div>
</div>

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

Bold font border

I am looking to enhance the appearance of text with a thick outline. After coming across a trick, I found the following code snippet: text-shadow: -1px -1px 0 #00f, 1px -1px 0 #00f, -1px 1px 0 #00f, 1px 1px 0 #00f; However, I noticed that ...

Creating a scrollable container using CSS

I am currently working on a React project and encountered an issue with the CSS styling. I am looking for a solution that allows me to keep the parent and child components the same while enabling a scroll effect on the left side of the div containing shopp ...

Bootstrap component malfunctioning and not performing as expected

Currently, I am learning how to use Bootstrap and attempting to replicate the example themes found on their official website. The specific theme I am focusing on is available at this link: https://getbootstrap.com/docs/4.3/examples/pricing/ Unfortunately, ...

Innovative dropdown menu featuring images and informative text snippets

I have a question about creating a menu similar to the one on http://www.resellerclub.com that includes images and text details. Are there any ideas on which technologies were used or if there are any commercial solutions available for this? Thank you. ...

Is it possible to nest a container tag within a container-fluid tag?

As I work on designing a website, I am wondering if it is possible to include a container tag within a container-fluid tag. Is this considered a best practice in web design? Any insights would be appreciated! ...

Ways to show error messages from a JSON reply within a DIV element

I am currently working on an AJAX form that allows users to change their passwords. My goal is to display all validation errors within an HTML DIV element. Although the form submission functionality is working well, I am facing an issue with passing the e ...

Tips for positioning divs at the bottom of the screen while making them inline and overlapping their parent div

Is there a way to make divs display at the bottom of the screen in a horizontal line, overlapping their parent div just like Facebook chat? I have attempted the following code but it doesn't seem to work. <div id="container"> <div class=" ...

Tips for arranging two divs from a single column to display side by side on smaller screens instead of stacked on top of each other

My knowledge of CSS, HTML, and bootstrap is quite limited. I'm trying to figure out how to split a single column on a large screen into two columns on a smaller screen so that the content in two divs appears side by side instead of stacked on top of ...

Utilizing the p tag to incorporate dynamic data in a single line

I am currently working with two JSON files named contacts and workers. Using the workerId present in the contacts JSON, I have implemented a loop to display workers associated with each contact. The current display looks like this: https://i.sstatic.net/AR ...

Design a background image that is optimized for both high-resolution retina displays and standard non-ret

Scenario I am working on a web page where I want the background image to be fixed, covering the entire screen or window (excluding tablets and smartphones). The background image has been created using ImageShack. Everything is running smoothly so far. T ...

Transform the styles from a React component into Cascading Style Sheets

I have been tasked with transitioning all the styles from the JavaScript file to the CSS file while ensuring the design remains intact. I am facing an issue where using the CSS styles breaks the search field design. The original design can be seen here: S ...

Generate a visually striking text overflow effect on an image

I'm looking to add a stylish hover effect to an image I created. The goal is to show text when the mouse hovers over the image, but I also want to include a background to enhance readability. Below is the HTML code: <div class="portfolio-frame"&g ...

Combining jQuery functions for a smooth user experience: fadeOut, live, and delay all working together in a div fade

Hello everyone, I am trying to make this div disappear as soon as it appears. It is currently working in this way, but occasionally the div is added after a load() function, so I'm thinking I may need to combine it with a live() method.... //Fade ou ...

Animating an image inside a bordered div

I'm attempting to create an animated effect where an image moves randomly within the boundaries of a div container. While I've come across solutions for animating within borders, none have specifically addressed keeping the image inside the div. ...

What is preventing me from opening this local html page without an IIS Server?

Currently immersed in a passion project that can be found at https://github.com/loganhenson/jsrpg This project has been a collaborative effort with a friend, utilizing Visual Studio Professional for development and testing it on my local IIS server. Init ...

I'm experiencing an issue where the graph created using Flot isn't being displayed

I attempted to create a basic graph using Flot in jQuery, but for some reason the graph is not appearing. Here is the HTML code for the page: <!doctype html> <head> <meta charset="utf-8" /> <meta name=“description” content=" ...

How to vertically center a div within a parent div with a fixed position?

I am facing an issue where I have a div positioned on top of another fixed position div, and I am looking for a way to vertically align the first div. Below is the code snippet that I am currently working with: <div class="overlay"> <div id=" ...

What is the best way to display SQL data in PHP using div elements?

I am currently working on a project that requires me to display posts from a database. Each post consists of a title and text content. I have successfully retrieved the necessary information from the database using PHP and SQL, however, I am struggling wit ...

The Z-index property does not seem to be functioning properly for a drop-down component that is located within a disclosure panel

Looking for a solution with my custom GWT drop down component, which includes a text box and vertical panel of check boxes. I need to integrate this component into a GWT disclosure panel. When the panel is expanded and the user interacts with the componen ...

What is the best way to sort out the <li> elements within a <ul> that has a specific id

<ul class="apple" id="A"> <li> <li> ..... ...... ...... </ul> <ul class="apple" id="C"> <li> <li> ...