What is the proper way to use the `or` operator in a CSS selector to target a specific child

Searching for a way to target a child element in the DOM using the CSS selectors below:

.parentA .child{

}

.parentB .child{

}

Is there a method to combine these selectors into one? I attempted the following but did not achieve the desired results:

.parentA,.parentB .child{

}

Answer №1

In the world of CSS, there is no room for the word OR. Instead, we have AND, and to achieve a similar effect, we use the comma (,) to separate multiple selectors.

So, when you need to apply the same styles to different selectors, you can list them out with commas like this:

.parentA .child,
.parentB .child {
  /* rules here */
}

Each selector will then be targeted individually. This method is much cleaner than writing them out separately as shown below:

.parentA .child {
  /* rules here */
}
.parentB .child {
  /* rules here */
}

To follow the DRY principle (Don't Repeat Yourself) in CSS, you could utilize SASS or other pre-processors to nest selectors like so:

.parentA, .parentB {
   .child {
     /* rules here */
   }
}

Keep in mind that SASS is a CSS pre-processor that translates into standard CSS during compilation.

Answer №2

Have you given this a shot?:

.elementA > .subelement, 
.elementB > .subelement{
}

Answer №3

If you're using plain CSS, you'll find yourself repeating the entire selector like this: .parentA .child, .parentB .child. However, with LESS, Sass, or other preprocessors, you can simplify it to:

.parentA, .parentB {
    .child {
         color: red;
    }
}

This will automatically compile back to the original selector for you, saving you from unnecessary repetition.

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

Converting Umbraco Json to a CSS class named "[]"

I'm currently using Umbraco with the data-type grid layout and I am trying to add custom settings (CSS classes) to each row/cell. The results are somewhat working, but I want to improve it. Here's the user interface: https://i.stack.imgur.com/iY ...

JavaScript- Tabbed Navigation with Lists as the Content

Currently, I am facing a frustrating issue in finding a suitable solution. My website uses tabs that utilize the UL, LI system, similar to most tab systems found in tutorials. The problem arises because the javascript on my site interferes with using the ...

Tips on adjusting the dimensions of a switch in kendo UI angular with CSS

Currently, I am utilizing angular in combination with Kendo ui. Is there a way to modify the dimensions of the switch button in Kendo UI Angular using CSS? ...

Two adjacent divs merge into the following div

Hey everyone, I'm facing an issue that I can't seem to figure out. I know it's probably something very simple, but as a beginner, I'm struggling... Every time I use the float:left command, the elements with classes .con-icon and #con-i ...

What is the best way to make my page headers resize with a responsive layout?

Currently, I am working on a website that needs to be fully functional on both mobile devices and computers. I have successfully implemented Bootstrap-responsive to make many elements work, but now I am focusing on the hero-unit for the homepage. Specifica ...

Is it possible for the data in checkboxes to update dynamically using HTML and CSS?

How can I create a form with checkboxes in HTML/CSS that functions similar to the one on this webpage: Desired Outcome (scroll down to see)The webpage features a form with checkboxes on the right side. When a checkbox is selected, the values on the left s ...

Managing color changes in Gmail while using dark mode

I've encountered some challenges with Gmail's dark mode on mobile clients, particularly regarding the color scheme it uses. When I try to use a specific green background color background-color: #00ff00;, Gmail in dark mode changes it to a darker ...

What is the best way to horizontally center a div that doesn't have a set width

I am a beginner in CSS and have been experimenting with various solutions from Stack Overflow, but so far none of them seem to be working for my specific case. Below is the parent div code from an existing application where I cannot control the inline sty ...

Issue with displaying image element over another element

I recently encountered a unique challenge with my SVG element, which acts as a container for a chessboard I developed. In certain key moments of the game, such as when a pawn is promoted and a player needs to choose a new piece, I found it necessary to hav ...

What could be the reason for the button not properly aligning on a mobile device?

I've been working on a webpage for my mother's students, and I'm facing some challenges. When I view the page on mobile, everything adjusts except for the buttons. They end up overlapping each other, creating a messy layout. Additionally, I& ...

Creating shapes with CSS and Vue

I've been attempting to replicate this design on my website using CSS and Vue, but I'm struggling to achieve the desired result. As a newcomer to this, I could really use some guidance. Here is my goal: https://i.sstatic.net/WI9Wr.png This is ...

Can you tell me the standard CSS easing curves?

When working with css, we have the option to incorporate easing for transitions, like this: transition: all .5s ease-in-out CSS provides predefined easings such as ease, ease-in, ease-out, and ease-in-out. We can also create a customized easing using a ...

The sticky navigation bar's background fails to change at the appropriate time following an image slider

I am currently facing an issue with my sticky navigation bar. I want the background color to change to white after the user scrolls past the image slider. The problem is that the white background appears above the image slider instead of in the orange logo ...

The div escapes the container and falls down to the one below it

I am encountering an issue with the layout of my outer container, which contains a column of numbers and animated text. The problem arises when the animated text, supposed to be beside the number column, drops under the numbers before bouncing back up as i ...

`Shaping up bootstrap spans with CSS`

I am working on a simple bootstrap layout for a workday scheduler app, and I've noticed that when the width of the text content increases it affects the vertical alignment of the spans where I display the hours of the day. Here is an image to illustra ...

"Utilizing Bootstrap to position the right column at the top on a mobile platform

I am in need of assistance with getting the correct div to display on top when viewed on a mobile device using Bootstrap. The issue is discussed and resolved in this particular discussion. To clarify, I would like my desktop layout to appear as follows: A ...

Store text in a table format in your local storage

I need help figuring out how to save product and price information to local storage when an "add to cart" button is pressed. Can someone provide guidance on how to do this? Here is the code I currently have: body> <!-- Header--> ...

How to apply dynamic styling to a MatHeaderCell using NgStyle?

My goal is to dynamically style a MatHeaderCell instance using the following code: [ngStyle]="styleHeaderCell(c)" Check out my demo here. After examining, I noticed that: styleHeaderCell(c) It receives the column and returns an object, however ...

The concept of transparency in CSS being utilized on a nested div element

Seeking clarity on the opacity property in CSS. My dilemma involves a header with a title, tinted with an opacity of .3 (in addition to a transition effect - irrelevant to the inquiry). Referencing the gif below: https://i.sstatic.net/LXwPv.gif I apprec ...

Background-color in CSS can be seen overlapping border on table cells in Internet Explorer

I am encountering an issue with a table containing two simple table cells: <table> <tr> <td>Test1</td> </tr> <tr> <td>Test2</td> </tr> </table> When I apply the following CSS to ...