0

I am working using nextjs, typescript, and scss. on global.scss there is *(asterisk selector) to set padding and margin to be zero(0) , but its not working

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

import "../styles/globals.scss";
import type { AppProps } from "next/app";
import "antd/dist/antd.css";

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />;
}

export default MyApp;
Shyn
  • 3
  • 1

4 Answers4

0

try :

    * {
         box-sizing: border-box !important;
         padding: 0 !important;
         margin: 0 !important;
      }
Mah Es
  • 572
  • 3
  • 6
  • Dont use `!important` for this. Why? [Here's why](https://stackoverflow.com/questions/3706819/what-are-the-implications-of-using-important-in-css). Use proper cascading instead. – Fabian S. Nov 18 '21 at 07:42
  • i just gave an answer specific to the question. question is not about whether its a good practice or not. @FabianS. – Mah Es Nov 18 '21 at 08:16
  • I know, i did not say your answer doesnt match the question cause _it does_. I just wanted to point out, that using `!important` this way is bad practise and can lead to huge problems later on. Should have said "like this" instead of "for this" that would have made that more clear. – Fabian S. Nov 18 '21 at 08:32
0

Try

body {
  padding: 0;
  margin: 0;
}
Fabian S.
  • 2,370
  • 2
  • 25
  • 33
shen s
  • 21
  • 3
  • This will only reset the `padding` and `margin` on the `body` tag. [padding](https://developer.mozilla.org/en-US/docs/Web/CSS/padding) and [margin](https://developer.mozilla.org/en-US/docs/Web/CSS/margin) are not inherited in css so this will only affect the `body` tag. – Fabian S. Nov 18 '21 at 07:39
0

Its because the selector's specitivity of * is less than h1,h2,h3,h4,h5,h6.

That means the h1,h2,h3,h4,h5,h6 selectors values will overwrite the values defined in *.

Yes, you could use !important to get around this by completely overruling any specitivity but that breaks the whole cascading feature (aka specitivity) of css and will haunt you at night. Why? Read this.

The proper solution on your issue would be to properly overwrite the margin on the h1,h2,h3,h4,h5,h6 selector by yourself in the cases you need it.

* {
  margin: 0;
  padding: 0;
}

h1,
h2 {
  /* this will overwrite the margin:0 and set the color to red from the * selector */
  margin-top: 10px;
  color: red;
}

.mycontentwrapper h1 {
  /* this will overwrite the margin:10px from the h1,h2 selector */
  margin-top: 0;
}

.mycontentwrapper h2 {
  /* this will overwrite the margin and color from the h1,h2 selector */
  color: blue;
  margin-top: 50px;
}
<div class="mycontentwrapper">
  <h1>Test</h1>
  <h2>Test 2</h2>
</div>
Fabian S.
  • 2,370
  • 2
  • 25
  • 33
-1

Try

html {
  box-sizing: border-box ;
  padding: 0px ;
  margin: 0px;
}
Fabian S.
  • 2,370
  • 2
  • 25
  • 33
  • This will only reset the `padding` and `margin` on the `html` tag. [padding](https://developer.mozilla.org/en-US/docs/Web/CSS/padding) and [margin](https://developer.mozilla.org/en-US/docs/Web/CSS/margin) are not inherited in css so this will only affect the `html` tag. – Fabian S. Nov 18 '21 at 07:40