1

I made some extremely basic html code, and for some reason there is a padding comming from nowhere : not what I want Now I don't understand at all why is there a background empty space between the top of the page and the top of the grey box, as well as on the sides of the grey box. Isn't Witdh: 100% supposed to take all the space available (A.K.A. 1920px instaed of 1904px) ?

the code :

<html lang= "fr">
<style>
    :root {
        --bgcolor: #101B20;
    }
    html {
        background-color: var(--bgcolor);
        width: 100%;
        height: 100%;
        border: 0px;
    }
    p {
        font-size: 16px;
        color: #FFFFFF;
        font-family:'Open Sans', sans-serif;
        text-align: center;
    }
    .sticky_square {
      height: 50px;
      width: 100%;
      background-color: #808080;
    }
</style>
<head>
    <link rel="icon" href="images/tarkov icon.png">
    <title>THIS IS A TEST</title>
    <meta charset="utf-8"/>
</head>

<body>
    <div class=sticky_square>
        <p>txt</p>
    </div>
    <p>txt2</p>
</body>
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
the shadow
  • 65
  • 7
  • The `

    ` tag features inherent margin, and/or some browsers provide a preceding blank line. [Another answer along these lines](https://stackoverflow.com/a/819190/6340496).

    – S3DEV Mar 18 '23 at 19:55
  • that's good to know, yet the

    is inside the div container so it shouldn't interact with the grey box

    – the shadow Mar 18 '23 at 19:56
  • 2
    The `

    ` element's margin is going out of its parent `

    `'s bounds. You can prevent this by giving the parent `
    ` the CSS rule `overflow: auto;`
    – JCollier Mar 18 '23 at 20:38

1 Answers1

2

The space you don't want is coming from two elements' default CSS: <p> and <body>.

Set both of their margins to 0 by adding

body, p {
    margin: 0;
}

into your CSS, and the space will go away.

Another solution could be to make it so the parent <div> doesn't allow the <p>'s margin to go outside of it (which is currently happening), while still getting rid of the <body>'s margin:

body {
    margin: 0;
}

.sticky_square {
    overflow: auto
}
JCollier
  • 1,102
  • 2
  • 19
  • 31