4

I've just learning HTML/CSS for a few days and I'm having trouble with this: enter image description here Why there is a blank line at the top of the page? Can someone tell me what was wrong or missing from my code? How can I fix it?

Here's my code:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    body {
      margin: 0;
      padding: 0;
    }
    
    header {
      text-align: center;
      background-color: lightgray;
    }
    
    header h1 {
      font-size: 70px;
    }
    
    ul {
      background-color: gray;
      padding: 10px;
    }
    
    li {
      display: inline;
      margin: 0 5px 0 5px;
    }
    
    a {
      color: white;
      text-decoration: none;
      text-transform: uppercase;
      font-size: 18px;
    }
  </style>
</head>

<body>
  <header>
    <h1>My Website</h1>
    <p>A sample website</p>
    <ul>
      <li><a href="#">HOMEPAGE</a></li>
      <li><a href="#">ABOUT ME</a></li>
      <li><a href="#">CONTACT</a></li>
    </ul>
  </header>
</body>

</html>

Thanks in advance!

Community
  • 1
  • 1
DunDev
  • 220
  • 1
  • 4
  • 8

5 Answers5

3

This occurs because of parent and first child margin collapsing between the h1 and the margin of its parent element(s)

One solution would be to add border: 1px solid lightgray or add padding, or you can reset the margin itself to zero - see demo below:

body {
  margin: 0;
  padding: 0;
}

header {
  text-align: center;
  background-color: lightgray;
  border: 1px solid lightgray;
}

header h1 {
  font-size: 70px;
}

ul {
  background-color: gray;
  padding: 10px;
}

li {
  display: inline;
  margin: 0 5px 0 5px;
}

a {
  color: white;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 18px;
}
<header>
  <h1>My Website</h1>
  <p>A sample website</p>
  <ul>
    <li><a href="#">HOMEPAGE</a></li>
    <li><a href="#">ABOUT ME</a></li>
    <li><a href="#">CONTACT</a></li>
  </ul>
</header>
kukkuz
  • 41,512
  • 6
  • 59
  • 95
1

It's because your h1 has margins. To solve the issue, you need to reset h1's margin-top like this:

h1 {
  margin-top: 0;
}

And here is the working snippet:

<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
    body{
        margin: 0;
        padding: 0;
    }

    header{
        text-align: center;
        background-color: lightgray;
    }

    header h1{
        font-size: 70px;
        margin-top: 0;
    }

    ul{
        background-color: gray;
        padding: 10px;
    }

    li{
        display: inline;
        margin: 0 5px 0 5px;
    }

    a{
        color: white;
        text-decoration: none;
        text-transform: uppercase;
        font-size: 18px;
    }
</style>
<body>
<header>
    <h1>My Website</h1>
    <p>A sample website</p>
    <ul>
        <li><a href="#">HOMEPAGE</a></li>
        <li><a href="#">ABOUT ME</a></li>
        <li><a href="#">CONTACT</a></li>
    </ul>
</header>
</body>
P.S.
  • 15,970
  • 14
  • 62
  • 86
  • Can you tell me why I should apply 'margin-top: 0;' to

    but not
    ?

    – DunDev Sep 16 '17 at 17:06
  • @DunDev, because in developer tools inspectoe we can see, that `header` has no margins, and `h1` has top and bottom margins, but only top margin breaks your markup. – P.S. Sep 16 '17 at 20:33
1

The above blank line is caused by the browsers default css style.

Use normalize.css to reset the CSS applied by all the browser to a common one. So that you have level playgroud to apply styles.

https://github.com/necolas/normalize.css/

anshu purohit
  • 176
  • 2
  • 10
1

To solve this issue you can add margin-top:-45px; in header class.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    body {
      margin: 0;
      padding: 0;
    }
    
    header {
      text-align: center;
      margin-top:-45px;
      background-color: lightgray;
    }
    
    header h1 {
      font-size: 70px;
    }
    
    ul {
      background-color: gray;
      padding: 10px;
    }
    
    li {
      display: inline;
      margin: 0 5px 0 5px;
    }
    
    a {
      color: white;
      text-decoration: none;
      text-transform: uppercase;
      font-size: 18px;
    }
  </style>
</head>

<body>
  <header>
    <h1>My Website</h1>
    <p>A sample website</p>
    <ul>
      <li><a href="#">HOMEPAGE</a></li>
      <li><a href="#">ABOUT ME</a></li>
      <li><a href="#">CONTACT</a></li>
    </ul>
  </header>
  
</body>

</html>
Aashish Kumar
  • 2,771
  • 3
  • 28
  • 43
0

It is margin of tag h1 make it zero in css.

header h1{
margin: 0px;
}
Mahesh Lad
  • 1,997
  • 1
  • 9
  • 8