Home > Mobile >  Override css styles in react js
Override css styles in react js

Time:12-05

I created an application in React using ant design library. In my CSS file, I added some base styles:

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

I expect that these styles should override all styles, but they don't override antd styles. Inspecting the p tag in the console I noticed that these styles:

 {
    margin-top: 0;
    margin-bottom: 1em;
} /// comes from antd.css
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

overide my previous styles. How to fix this (to make my styles to override all styles) without using !important?
demo: https://codesandbox.io/s/antd-create-react-app-forked-0vs14?file=/index.js:315-811

CodePudding user response:

You only need to add px in number value

Based on antd github issue You need to improve your CSS selector's specificity. And it's not a bug.. Github Discussion

p {
  box-sizing: border-box;
  margin: 0px;
  padding: 30px;
}

CodePudding user response:

Try this:

html * {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

This is how CSS works, since antd styles target directly the p tag, you selecting it via * won't have a higher importance unless you add behind it html * or body * or mark the styles as !important

Working CodeSandbox that I forked from yours.

  • Related