CSS - apply unique styles to specific nodes based on the class they share

Struggling to apply different styles to the same class when it appears for the second time in the markup using the Subsequent-sibling combinator "~". It seems like there may be a detail I'm missing with the use of "~". Unfortunately, the HTML cannot be changed as it is generated by a CMS.

<div class="root">
  <div class="container">
    <div class="mosaic big">
      <div>Red</div>
    </div>
  </div>
  <div class="container">
    <div class="mosaic medium">
      <div>3</div>
    </div>
  </div>
  <div class="container">
    <div class="mosaic big">
      <div>Blue</div>
    </div>
  </div>
  <div class="container">
    <div class="mosaic medium">
      <div>4</div>
    </div>
  </div>
</div>

This is the CSS code:

.root > .container > .mosaic.big {
  color: red;
}
.root > .container > .mosaic.big ~ .root > .container > .mosaic.big {
  color: blue;
}

Check out the jsfiddle here. Thank you in advance.

Answer №1

Give this style a shot.

.parent > .wrapper > .grid.large {
  color: green
}

.parent > .wrapper:nth-child(3) > .grid.large {
  color: yellow
}

OR

.parent > .wrapper > .grid.large {
  color: green
}

.parent > .wrapper:nth-child(n+2) > .grid.large {
  color: yellow
}

Answer №2

Based on the suggestions from other responses, it seems like this is what you are looking for (feel free to provide feedback if it doesn't cover all scenarios):

Just as mentioned before, the ~ selector only applies to elements with the same parent.

Keep in mind that the text isn't turning blue because it's the second instance of .mosaic.big within a container, but rather because the container itself is not the first one. If you want a fully functional solution without relying on experimental features, JavaScript might be necessary.

CSS:

.root > .container > .mosaic.big {
  color: red
}

.root > .container ~ .container > .mosaic.big {
  color: blue
}

https://jsfiddle.net/wd1rhg11/5/

Answer №3

Unfortunately, it is not possible to utilize the subsequent sibling selector without altering the html code.

The CSS next sibling selector only targets the immediate siblings of the preceding class. Therefore, in order for this to function correctly, the elements must share the same parent.

Furthermore, the matching on the right-hand side will begin at the element matched on the left-hand side, eliminating the need to include the parent structure.

.root > .container > .mosaic.big {
  color: red
}

.root > .container > .mosaic.big ~ .mosaic.big {
  color: blue
}

For an updated example, please refer to:

https://jsfiddle.net/wd1rhg11/4/

If feasible, consider utilizing javascript to achieve your desired outcome.

Answer №4

To target specific elements with the '.mosaic' class, you can use the nth-child(n) selector:

.root > .container > .mosaic:nth-child(1).big {
  color: green;
}
.root > .container > .mosaic:nth-child(3).big {
  color: yellow;
}

Check out this jsfiddle for a live example: https://jsfiddle.net/andrewsilent/x4t7neya/

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 there a way to align two links side by side, with one on the left and the other on the right?

<a href="example"><p style="text-align:left">Previous Page</a> <a href="example"><p style="text-align:right">Next Page</a> I am trying to figure out how to align these links on the same line. Any suggestions would be a ...

How can I ensure that a div is positioned over another div with the same coordinates across all screen sizes?

[http://jsfiddle.net/w7r3gveg/]1 I am looking to position 4 Facebook logos in the center bottom of my background image. The background is represented by the 'bg' div, while the Facebook logo is in the 'facebook' div. Currently, I can ...

JavaScript Button with the Ability to Input Text in a TextArea?

For instance, imagine a scenario where you click on a button and it then displays various options for you to select from. Whatever option you pick will be automatically inserted into the text area. ...

Unusual occurrences observed with table cell padding in CSS

When I include a TD padding in my CSS like this: #maincontent table td, #maincontent table th { padding: .333em 0 .333em .25em; } In Safari and IE, the padding is applied correctly to the TD, but not in Firefox. If I apply the TD padding only to th ...

Is it necessary for HTML "buttons" to follow the same box-model as other elements?

I recently encountered an issue with the button element and the input element when using a type of button. In both Firefox and Chrome, I noticed a behavior that seems like a bug in recent releases. However, as form elements often have exceptions to W3 rule ...

Setting column heights and spacing in Bootstrap

I'm looking to dynamically populate the blocks. Essentially, a for loop would be used to pass the data. However, I'm facing some challenges with the height of the blocks at different screen sizes while utilizing bootstrap 4. My goal is to have th ...

Chakra UI: Trouble with Styling Multi-part Components - StyleProvider Missing

Attempting to style multipart components as outlined in this documentation is proving to be challenging. Despite my efforts, I keep encountering the following error: ContextError: useStyles: styles is undefined. It appears that I may have forgotten to wr ...

What is the best way to enlarge a column contained within a container, allowing an image to spread from the center of the column all the way to the right edge of the page?

I am trying to modify my image within a bootstrap container to extend it to the side while keeping the rest of the layout unchanged. Here is what I currently have: https://i.sstatic.net/Mcukl.jpg. There is a gap on the right side of the image that I want t ...

JavaScript code that moves the active link to the top of the navigation when the window width is less than or equal to 800px

I'm working on a responsive navigation that is fixed at the top and switches from horizontal to vertical when the screen size is less than or equal to 800 pixels wide. However, I'm facing an issue with moving the active link to the top of the na ...

Creating a soft focus effect in a specific region (behind the text)

While working on my homepage created with HTML/CSS/Javascript, I have text positioned at the top left corner of the screen. The challenge arises from the fact that I am using different backgrounds sourced from Reddit, and a script randomly selects one duri ...

Issues are arising with CSS .not(selector) functionality when applied to a span element

One of the challenges I'm facing is formatting a header with a span tag that contains a date and some accompanying text. The goal is to make the text bold without affecting the formatting of the date. In my CSS file, I have tried using :not(cls) to t ...

Changes in browser size will not affect the height

Is there a way to smoothly change the height of the navigation bar when resizing the browser? Currently, it snaps to the new height when the width goes below or above 1000px. Any suggestions? nav.cust-navbar { -webkit-transition: height 2s; tran ...

Managing post requests within the express.js framework

I need assistance utilizing the value provided in a form within Node.js. Here is an example: index.html : <!DOCTYPE html> <html lang="en"> <head> </head> <body> <div align="middle" > <!--Ethernet ...

Automatically modify the data types of elements within an array

For a recent project, I created a class using TypeScript and React to handle images. Here is an example of how my class looks: class MyCustomImage extends Image { oriHeight: number; } Once I uploaded two images, I ended up with an array called 'r ...

Combining PHP and MySQL with a join statement

I am trying to calculate the average rating for each store in a directory list using MySQL's AVG function. Whenever someone reviews a store, they assign a rating ranging from 1 to 5. This rating is then inserted into the rating column of the reviews ...

Learn how to retrieve the value of an associated field at a specific index by utilizing a combo box in JavaScript when receiving a JSON response

Hey there, I'm currently working on a phone-gap app where I need to fetch data from a WCF service that returns JSON responses. Specifically, I want to display the DesignName in a combo box and pass the associated designId. Any thoughts on how I can ac ...

Leveraging the power of AJAX/JQuery to send form data to a PHP script

I've compiled this based on information from other sources on this platform and a variety of tutorials. I'm puzzled as to why it's not functioning, and I've had no success with alternative methods. Below are excerpts from the HTML of m ...

I am encountering difficulties in utilizing Selenium with Python to access the dropdown menu

Trying to create a Python script using Selenium to automate the registration process for a sample event I have organized. Click here to view the event page. Successfully accessed the page and clicked on the "Join the guestlist" button: from selenium impor ...

How to Align Horizontal Scrollable Cards in Bootstrap 4

Having trouble centering a Single Row of Horizontal Scrolling Cards. I've tried: a) the mx-auto class b) using text-align: center Nothing seems to be working and there's a lot of space on the right side in desktop view. Interestingly, the scro ...

How can CSS be used to center multi-line text with the shortest line appearing at the top

I'm currently working on some html and css code that looks like this: <div style="width: 40em; text-align: center;"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard du ...