简体   繁体   English

如何将 !important 添加到 CSS-in-JS (JSS) 类属性中?

[英]How do you add !important to a CSS-in-JS (JSS) class property?

I am trying to use some CSS-in-JS classes from this answer with a material UI component in my React project.我正在尝试将此答案中的一些 CSS-in-JS 类与我的 React 项目中的材质 UI 组件一起使用。 I need to override the CSS coming from Bootstrap so I want to use the !important modifier, I've only used this in .css files before and I'm unsure how to do it in CSS-in-JS.我需要覆盖来自 Bootstrap 的 CSS,所以我想使用 !important 修饰符,我之前只在 .css 文件中使用过它,我不确定如何在 CSS-in-JS 中做到这一点。 My styles object to be passed into the Material-UI withStyles function looks like the following, how do I add !important to the fontSize attribute?我要传递到 Material-UI withStyles函数的样式对象如下所示,如何将 !important 添加到 fontSize 属性? I've tried 30 !important and a few other things but nothing seems to work.我已经尝试了30 !important和其他一些东西,但似乎没有任何效果。

Thanks谢谢

const styles = {
  labelRoot: {
    fontSize: 30
  }
}

You shouldn't need to do this as Adrian points out, but if you absolutely need to do it you can set it to a string:正如 Adrian 指出的那样,您不需要这样做,但如果您绝对需要这样做,您可以将其设置为字符串:

const styles = {
  labelRoot: {
    fontSize: '30px !important',
  },
};

You can use the array-based syntax for space and comma separated values.您可以对空格和逗号分隔值使用基于数组的语法

Just do this:只需这样做:

const styles = {
  labelRoot: {
    fontSize: [[30], '!important'],
    margin: [[5, 10], '!important']
  }
}

You can style !important the same way inline that you would in css.您可以使用与在 css 中相同的内联方式设置!important样式。

In the below example, both divs are styled blue !important in css, but the pink div has important also inline, and so takes precedence.在下面的例子中,两个 div 在 css 中都是blue !important

 div { width: 200px; height: 200px; background: blue !important; flex:1; } section{display:flex;}
 <section> <div style="background: red;"></div> <div style="background: pink !important;"></div> </section>

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

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