What does :global (colon global) do?

荒凉一梦 提交于 2019-12-04 16:10:46

问题


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.


回答1:


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.




回答2:


It looks like they are using CSS Modules. If you follow the docs they say:

:global switches to global scope for the current selector resp. identifier. :global(.xxx) resp. @keyframes :global(xxx) declares the stuff in parenthesis in the global scope.



来源:https://stackoverflow.com/questions/43613619/what-does-global-colon-global-do

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!