56

I'm new to web development, and met a problem when removing margin of body.

There's space between the very top and "logo" There's space between the very top of the browser and "logo" text. And my code is here on jsbin.

Is body { margin: 0;} wrong if I'd like to remove the space?

potashin
  • 44,205
  • 11
  • 83
  • 107
chenghuayang
  • 1,424
  • 1
  • 17
  • 35

10 Answers10

82

I would say that using this global reset is a bad way of solving this.

* {
  margin: 0;
  padding: 0;
}

The reason for the h1 margin popping out of the parent is that the parent does not have a padding.

If you add a padding to the parent element of the h1, the margin will be inside the parent.

Resetting all paddings and margins to 0 can cause a lot of side effects. Then it's better to remove margin-top for this specific headline.

isherwood
  • 58,414
  • 16
  • 114
  • 157
andeersg
  • 1,505
  • 2
  • 13
  • 24
  • What kind of side effects? The only thing I know is that it might be better for the performance to reset like this: body, div, p, a, img, h1, h2....{padding:0; margin:0;} –  May 13 '15 at 08:24
  • 1
    You have to do more work to reapply margins and/or padding to the elements you want it on (You get full control, but you have to do more work). Of course, all of this is preferences by the person coding, but for me i like "normalize.css" better than "reset.css". – andeersg May 13 '15 at 08:29
  • Yup. I've heard of this before. Maybe trying to use normalize is more targeted for resetting margin/padding. Thanks! – chenghuayang May 13 '15 at 14:23
  • Thanks andeersg for the tip to add padding to the body to prevent any margins overlapping it. That one tip has solved so many layout issues I've experienced that it deserves a web page and shrine dedicated to it! – PapillonUK Nov 21 '15 at 12:37
13

Some HTML elements have predefined margins (namely: body, h1 to h6, p, fieldset, form, ul, ol, dl, dir, menu, blockquote and dd).

In your case it's the h1 causing your problem. It has { margin: .67em } by default. If you set it to 0 it will remove the space.

To solve problems like these generally, I recommend using your browser's dev tools. For most browsers: right-click on the element you want to know more about and select "Inspect Element". In the "Styles" tab, at the very bottom, you have a CSS box-model. This is a great tool for visualising borders, padding and margins and what elements are the root of your styling headaches.

DJ_Icebear
  • 203
  • 2
  • 8
12

I would recommend you to reset all the HTML elements before writing your css with:

* {
    margin: 0;
    padding: 0;
} 

After that, you can write your custom css, without any problems.

Mystical
  • 2,505
  • 2
  • 24
  • 43
Spawn
  • 326
  • 1
  • 8
  • This is a heavy-handed and problematic approach. It requires subsequent re-styling of paragraphs, lists, etc. It's simply not necessary. – isherwood Mar 10 '23 at 14:15
6

You've still got a margin on your h1 tag

So you need to remove that like this:

h1 {
 margin-top:0;
}
6

The issue is with the h1 header margin. You need to try this:

h1 {
 margin-top:0;
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
5

This should help you get rid of body margins and default top margin of <h1> tag

body{
        margin: 0px;
        padding: 0px;
    }

h1 {
        margin-top: 0px;
    }
Mladen Oršolić
  • 1,352
  • 3
  • 23
  • 43
3

Just Remove The Browser Default Margin and Padding Apply Top Of Your Css.

<style>

* {
  margin: 0;
  padding: 0;
}

</style>

NOTE:

  • Try to Reset all the html elements before writing your css.

OR [ Use This In Your Case ]

<style>

  *{
        margin: 0px;
        padding: 0px;
    }

h1 {
        margin-top: 0px;
    }

</style>

DEMO:

<style>

  *{
        margin: 0px;
        padding: 0px;
    }

h1 {
        margin-top: 0px;
    }

</style>
<html>
 <head>
 </head>
 <body>
  <h1>logo</h1>
 </body>
</html>
RïshïKêsh Kümar
  • 4,734
  • 1
  • 24
  • 36
1

You can use body or * to make margin and padding 0px;

*{
margin: 0px;
padding:0px;
}
Vinayak
  • 415
  • 3
  • 21
1

The right answer for this question is "css reset".

* {
    margin: 0;
    padding: 0;
}

It removes all default margin and padding for every object on the page, no holds barred, regardless of browser.

Darian
  • 348
  • 1
  • 8
0

I found this problem continued even when setting the BODY MARGIN to zero.

However it turns out there is an easy fix. All you need to do is give your HEADER tag a 1px border, aswell as setting the BODY MARGIN to zero, as shown below.

body { margin:0px; }

header { border:1px black solid; }

You may also need to change the MARGIN to zero for any H1, H2, etc. elements you have within your HEADER div. This will get rid of any extra space which may show around the text.

Not sure why this works, but I use Chrome browser. Obviously you can also change the colour of the border to match your header colour.

wardheed
  • 91
  • 4