Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does :global (colon global) do?

In some SCSS files, I see the following:

:global {   /* ... */ } 

I don't know if it is an SCSS feature or a CSS feature. I tried searching about it but couldn't find any good results at first sight.

like image 717
wellbeck190 Avatar asked Apr 25 '17 14:04

wellbeck190


People also ask

What does global CSS do?

Traditionally, websites are styled using global CSS files. Globally-scoped CSS rules are declared in external . css stylesheets, and CSS specificity and the Cascade determine how styles are applied.

What is global selector?

In Stylable, selectors are namespaced to the stylesheet. However there might be cases where you want to target global selectors without Stylable namespacing them, For that you can use the :global() selector.

What is module SCSS?

module. scss is SCSS file with CSS modules. According to the repo, CSS modules are: CSS files in which all class names and animation names are scoped locally by default.

How do you use modules in react 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

This operator is used in CSS Modules. Modular CSS uses a CSS Modules compiler to scope CSS styles within their respective modules (e.g., React component).

Here's an example from a React module (in the file ErrorMessaging.less for the ErrorMessaging.jsx React component):

:global(.ocssContainer) {   .ui_column {     padding-left: 0;   } } 

This gets compiled into:

  .ErrorMessaging__alertContainer--1I-Cz .ocssContainer .ErrorMessaging__ui_column--3uMUS {     padding-left: 0;   } 

But now I add a :global modifier onto .ui_column:

:global(.ocssContainer) {   :global(.ui_column) {     padding-left: 0;   } } 

And this is what it compiles to:

  .ErrorMessaging__alertContainer--1I-Cz .ocssContainer .ui_column {     padding-left: 0;   } 

Now .ui_column can apply to any child element with that style, including in a child React component, and not just .ui_column elements that are part of the ErrorMessaging React component.

like image 186
JohnK Avatar answered Sep 24 '22 07:09

JohnK