0

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;
}

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

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

9
  • 1
    Where are you trying to override the styles? In CSS you can override styles by making the selector (the thing that says what element is affected) more specific. css-tricks.com/specifics-on-css-specificity Commented Dec 4, 2021 at 13:05
  • @evolutionxbox, in styless.css i added the above code that should remove all margins, but antd styles override mine? how to fix? Commented Dec 4, 2021 at 13:09
  • yours should be loaded after the antd.css file Commented Dec 4, 2021 at 13:11
  • 1
    and if antd puts the styles on the p-tag you have to be more spcific, as evolutionxbox said Commented Dec 4, 2021 at 13:12
  • p { box-sizing: border-box; margin: 0; padding: 0; } Commented Dec 4, 2021 at 13:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

10 Comments

your answer does not work
it will not work, as style is not specific. this way you can only stet styles that are not set more specific
You watch codesandbox that i send ?
you can set the padding, as it is not send via a p style, but you cant set the margin this way.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.