0

I have the following html page:

<!DOCTYPE html>
<html>

<head>
  <style>
    html {
      height: 100%;
    }
    body {
      margin: 0;
      padding: 0;
      height: 100%;
    }

    .box {
      height: 100%;
      background-color: brown;
    }
  </style>
</head>

<body>
  <div class="box">
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
  </div>
</body>

</html>

As you can see, there a distance on top, although body is set to:

  margin: 0;
  padding: 0; 
Johannes
  • 64,305
  • 18
  • 73
  • 130
softshipper
  • 32,463
  • 51
  • 192
  • 400
  • 2
    That's the default margin of the `p` element which breaks out of the DIV ("collapsing margins") – Johannes Jun 01 '18 at 14:24
  • I would argue that the _better_ "duplicate" answer is this one: https://stackoverflow.com/questions/1828804/how-do-i-uncollapse-a-margin – random_user_name Jun 01 '18 at 14:38

4 Answers4

1

This is due to collapsing margins.

Essentially, the p margin is combined with the parent div margin (and then again with the body margin), to push the body down.

There are (at least) 3 ways to solve this:

  1. Remove the margin-top from the p, which has the negative consequence of removing the margin for all p tags.
  2. Add border-top or padding-top to the container ( .box ), which has the negative consequence of adding padding or border that you may not want.
  3. Add overflow: [something] such as overflow: hidden to the .box container, which has the potential consequence of altering overflow in an undesired way.

<!DOCTYPE html>
<html>

<head>
  <style>
    html {
      height: 100%;
    }
    
    body {
      margin: 0;
      height: 100%;
    }
    
    .box {
      /* padding-top on the container is one solution */
      padding-top: 1px;
      height: 100%;
      background-color: brown;
    }
  </style>
</head>

<body>
  <div class="box">
    <p>This is a paragraph.</p>
    <p>This is a paragraph.</p>
  </div>
</body>

</html>
random_user_name
  • 25,694
  • 7
  • 76
  • 115
1

That's the default margin of the p element which breaks out of the DIV ("collapsing margins"). If you set margin: 0 for p, it will disappear, however, p elements will have less vertical distance between them then.

Johannes
  • 64,305
  • 18
  • 73
  • 130
0

Paragraphs have padding built in by default, you can either do an inline style to override it or specify that Paragraphs should have no padding by doing a global default.

<head>
  <style>
    html {
      height: 100%;
    }
    body {
      margin: 0;
      padding: 0;
      height: 100%;
    }

    .box {
      height: 100%;
      background-color: brown;
    }
  </style>
</head>

<body>
  <div class="box">
    <p style='margin:0; padding:0;'>This is a paragraph.</p>
    <p style='margin:0; padding:0;'>This is a paragraph.</p>
  </div>
</body>

</html>

Do margin and padding to be sure.

OR......

If you don't want to do this inline change your style definition by adding this after your existing .box {} class:

.box p {
  margin:0; padding:0;
}

This will apply the style to all "P" tags within the class box

Antony
  • 51
  • 2
  • It doesn't look like it needs to be production ready code! Yikes let's not get too purist lol :-) – Antony Jun 01 '18 at 14:28
  • So, keep in mind that your answer will live on for a long time. Demonstrating a solution to a problem with a less-than-desirable method (inline styles) is something many future visitors will see. So yes, I do tend to be purist, OR at least explain in the answer that this may not be desirable, but it's one way to accomplish it.... – random_user_name Jun 01 '18 at 14:33
  • I do like Purist and is my normal mode.. right you are cale_b for calling me out on it however that said there are times when doing this inline is desired like when manipulating the DOM dynamically and you don't want to change an entire stylesheet(s) – Antony Jun 01 '18 at 14:35
0

You can set padding top of the .box

.box {
  padding-top: 1rem;
} 
relytmcd
  • 234
  • 1
  • 9