Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a global parent selector with CSS Modules

I'm using CSS Modules within a React application. I also have a dropdown component with some global styles (which I'm happy with, as the general styles I want to re-use).

When the dropdown is active, a CSS class is applied (.dropdown--active). Is there a way I can include that global class alongside my component's locally scoped styles? i.e., what I'd like is for this to work:

.myClass {
  color: red;
}

:global .dropdown--active .myClass {
  color: blue;
}

However, that syntax makes the entire selector global, which is not what I'm after: I want .myClass to be scoped to the component.

like image 468
CherryFlavourPez Avatar asked Sep 23 '16 14:09

CherryFlavourPez


People also ask

What is global selector in CSS?

Universal Selector CSS universal selectors select any type of elements in an HTML page. It matches a single element. An asterisk ( i.e. "*" ) is used to denote a CSS universal selector.

Is there a parent selector in CSS?

There is currently no way to select the parent of an element in CSS, at least not a way that works across all browsers. If there was a way to do it, it would be in either of the current CSS selectors specs: Selectors Level 3 Spec.

How do I select a parent class in CSS?

The element>element selector is used to select elements with a specific parent. Note: Elements that are not directly a child of the specified parent, are not selected.

How do you use modular CSS?

To import a CSS Module into the corresponding component, import the css modules stylesheet as styles or [name]Styles : In JSX, use the defined CSS class as a className prop like so: From here, you can add as many other CSS modules you'd like, just remember to import each as a different name.


1 Answers

just include the desired global class in parens:

:global(.dropdown--active) .myClass {
  color: blue;
}
like image 163
Pascal Duez Avatar answered Oct 14 '22 05:10

Pascal Duez