-1

I have this code only (no CSS or more HTML).

When I load the html file in my browser there seems to me a 21px margin at the top (see the image link if you don't understand what I mean).

I know that the body element normally has an 8px margin by default, so I removed the margin by adding this code through the developer tools. margin: 0px;

Even after adding that code, I still get the 21px margin at the top of the page.

I don't know what to do, so if you can please help me, that would me great.

body {
  margin: 0px;
}
<body>
  <div id="unit-type">
    <h1>Converter</h1>
  </div>
</body>
isherwood
  • 58,414
  • 16
  • 114
  • 157
  • Headings have a default margin as well. In your example, `h1 { margin: 0; }` should remove it – j08691 Aug 29 '22 at 19:35
  • 1
    You're apparently familiar with your browser's document inspector. Why weren't you able to see which element has the margin on it? – isherwood Aug 29 '22 at 19:39
  • I didn't know a margin with the header will make the body have a margin, but thanks, I'll do that next time. –  Aug 29 '22 at 19:42
  • FYI, units are unnecessary on zero values. CSS linters will actually flag that. Just use `0`. – isherwood Aug 29 '22 at 19:43
  • It _doesn't_ make the body have a margin. The heading's margin is _inside_ the body. Margin is outside. You could describe it as padding, but it's not that, either. – isherwood Aug 29 '22 at 19:43
  • but if the margin is inside the body, why does it show outside of the body? I would expect there to be a gap between the body and heading, but not the body and top of the page. –  Aug 29 '22 at 19:47
  • 1
    [This question/answer](https://stackoverflow.com/questions/1762539/margin-on-child-element-moves-parent-element) should help with understanding what's going on. In your case, you could add `overflow: auto` to the wrapper around the h1, that way (1) the margins won't collapse and (2) the body element will contain the margins as you are expecting. – chazsolo Aug 29 '22 at 19:59

3 Answers3

1

You can create a file to reset the page's CSS and import it into your HTML; an example of reset.css would be:

    html, body, div, span, applet, object, iframe,
    h1, h2, h3, h4, h5, h6, p, blockquote, pre,
    a, abbr, acronym, address, big, cite, code,
    del, dfn, em, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var,
    b, u, i, center,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td,
    article, aside, canvas, details, embed, 
    figure, figcaption, footer, header, hgroup, 
    menu, nav, output, ruby, section, summary,
    time, mark, audio, video {
        margin: 0;
        padding: 0;
        border: 0;
        font-size: 100%;
        font: inherit;
        vertical-align: baseline;
    }
    /* HTML5 display-role reset for older browsers */
    article, aside, details, figcaption, figure, 
    footer, header, hgroup, menu, nav, section {
        display: block;
    }
    body {
        line-height: 1;
    }
    ol, ul {
        list-style: none;
    }
    blockquote, q {
        quotes: none;
    }
    blockquote:before, blockquote:after,
    q:before, q:after {
        content: '';
        content: none;
    }
    table {
        border-collapse: collapse;
        border-spacing: 0;
    }

With this CSS, all default formatting that the browser puts will be removed.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

* {
  margin: 0px;
  padding:0px;
}
<body>
  <div id="unit-type">
    <h1>Converter</h1>
  </div>
</body>
  • That's a heavy-handed approach to a specific problem. It has repercussions for paragraphs, lists, etc. Not recommended – Noam Smadja Sep 02 '22 at 21:24
-2

Consider inspecting elements to determine which has the default margin or padding applied and replace * with the class, id, or element.

Using * resets default padding and margin for ALL elements (traditionally not best practice)

*{
margin: 0;
padding: 0;
}
  • 3
    That's a heavy-handed approach to a specific problem. It has repercussions for paragraphs, lists, etc. Not recommended. – isherwood Aug 29 '22 at 19:38