2
  1. body has margin: 0
  2. p tags has margin-top: 100px;

p tags affects body's margin-top why?

I expected margin-top:100px only affects p tags.

///////////////////////////////////////////////////////

I wonder why should I use padding-top instead of margin-top?

Margins create extra space around an element. In contrast, padding creates extra space within an element.

In this case p tags are located in body so margin and padding should be looks same

enter image description here https://stackblitz.com/edit/react-lktg4q?file=style.css

html, body {
  margin: 0;
  height: 100%;
}

h1, p {
  font-family: Lato;
  margin: 0;
  margin-top:100px;
}

#root {
  background: blue;
  height: 100vh;
}
      <div>
        <Hello name={this.state.name} />
        <p>
          Start editing to see some magic happen :)
        </p>
      </div>
JillAndMe
  • 3,989
  • 4
  • 30
  • 57

4 Answers4

4

That's the effect of margin collapsing. According to that MDN page:

Parent and first/last child

If there is no border, padding, inline part, block formatting context created, or clearance to separate the margin-top of a block from the margin-top of its first child block; or no border, padding, inline content, height, min-height, or max-height to separate the margin-bottom of a block from the margin-bottom of its last child, then those margins collapse. The collapsed margin ends up outside the parent.

There are many ways to go about trying to work around it. But understanding this concept is one step to doing it.

In your case, the margin of the first element in your body (which is h1) affects the margin of the body. If you wish to not have a margin on the first h1, you could add this (see it on this jsfiddle):

h1:first-child {
  margin-top: 0;
}

This assume your first element in the body is an h1.

Community
  • 1
  • 1
Frank Fajardo
  • 7,034
  • 1
  • 29
  • 47
0

Apply padding-top instead of margin-top

html,
body {
  margin: 0;
  height: 100%;
}

h1,
p {
  font-family: Lato;
  margin: 0;
  padding-top: 100px;
}

#root {
  background: blue;
  height: 100vh;
}
<div id="root">
  <div>
    <h1>Hello React!</h1>
    <p>Start editing to see some magic happen :)</p>
  </div>
</div>
Sai Manoj
  • 3,809
  • 1
  • 14
  • 35
0

I Have updated your code.

StackBlitz

Sumit Patel
  • 4,530
  • 1
  • 10
  • 28
0

Looking at this,

#root {
  background: blue;
  height: 100vh;
}

I can say that you want background color to your body, as #root is the parent element and no element can be made parent to this, you can add #root css to your html, body css,

html, body {
  margin: 0;
  height: 100vh;
  background: blue;
}

h1, p {
  font-family: Lato;
  margin: 0;
  margin-top:100px;
}

Now coming to the scroll on the page, the scroll is coming because of margin-top:100px. You can fix this in two ways.

1st Way,

If margin-top:100px is the fixed layout throughout your application then,

html, body {
  margin: 0;
  height: calc(100vh-100px); //minus your margin from viewport height will eliminate scroll
  background: blue;
} 

2nd way,

Replace margin-top with padding-top, because padding is included in element height and margin not.

h1, p {
  font-family: Lato;
  margin: 0;
  padding-top:100px;
}
ravibagul91
  • 20,072
  • 5
  • 36
  • 59