简体   繁体   English

React 本地化全局样式?

[英]React Localize Global Styles?

Is it possible to localize the global styles of a module to only one file?是否可以将模块的全局样式本地化到一个文件?

for instance, I would like to change the width of an item from a react module but only for one file.例如,我想从反应模块更改项目的宽度,但仅限于一个文件。 inline styles aren't possible :/内联样式是不可能的:/

edit for context: in my particular case ... i'm using 'react-multi-carousel' ... and to go over the breakpoint behavior, I've set .react-multi-carousel-item { width: 310px !important; }编辑上下文:在我的特殊情况下......我正在使用'react-multi-carousel'......并且为了检查断点行为,我设置.react-multi-carousel-item { width: 310px !important; } .react-multi-carousel-item { width: 310px !important; } . .react-multi-carousel-item { width: 310px !important; } However i'd like this style to be applied only to one component and not my entire project (I'm using the carousel in more than one place).但是,我希望这种样式仅应用于一个组件,而不是我的整个项目(我在多个地方使用轮播)。 Any way to localize a global style (CSS file)?有什么方法可以本地化全局样式(CSS 文件)?

You just nest the CSS rule to only apply when inside a specific container:您只需嵌套 CSS 规则以仅在特定容器内应用:

.my-container .react-multi-carousel-item { width: 310px !important; }

And then in React:然后在 React 中:

return <>
  <MultiCarousel />
  <div className="my-container">
    <MultiCarousel />
  </div>
</>

In that example the first <MultiCarousel /> would not see the extra style, but the second one would.在该示例中,第一个<MultiCarousel />不会看到额外的样式,但第二个会。


"is there any way to add css within the TSX file by any chance? If possible, I'd like to not have an external css file." “有没有办法在 TSX 文件中添加 css?如果可能的话,我希望没有外部 css 文件。”

You could try a scoped style tag您可以尝试使用范围样式标签

For example:例如:

return <>
  <div>
    <style scoped>{`
      .react-multi-carousel-item {
        width: 310px !important;
      }
    `}</style>
    <MultiCarousel />
  </div>
</>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM