Home > Software design >  White Space in CSS / HTML [duplicate]
White Space in CSS / HTML [duplicate]

Time:10-01

I have a test file where there is white space at the top of the header and footer DIVs. Is this because the DIV does not have a defined border? If I do add a border and color it the same as the DIV background, the white space is corrected. If I also remove the top margins of those elements in the header and footer, the problem goes away.

But, I thought if I added position-relative to the those containers, that the element properties like default margin would be relative against the container and not the body.

Any help would be appreciated. Thank you

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

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Div Testing</title>

  <style>
    body {
      margin: 0%;
      padding: 0%;
    }
    
    h1 {
      text-align: center;
    }
    
    p {
      text-align: justify;
      padding: 25px;
    }
    
    .header-container {
      position: relative;
    }
    
    .header {
      display: block;
      height: 150px;
      background-color: rgb(206, 231, 233);
    }
    
    .div1 {
      display: block;
      float: left;
      width: 33.33%;
      /* border-style: solid;
                border-width: 1px; */
      background-color: #9fc9cd;
    }
    
    .div2 {
      display: block;
      float: left;
      width: 33.33%;
      /* border-style: solid;
                border-width: 1px; */
      background-color: #ffecc5;
    }
    
    .div3 {
      display: block;
      float: left;
      width: 33.33%;
      /* border-style: solid;
                border-width: 1px; */
      background-color: #ffc8c5;
    }
    
    .clear {
      clear: both;
    }
    
    .footer {
      display: block;
      position: relative;
      text-align: center;
      height: 100px;
      background-color: #689fa4;
    }
    
    #footer-h2 {
      position: relative;
      top: 25px;
    }
  </style>
</head>

<body>
  <div class="header-container">
    <div class="header">
      <h1>Title Here!</h1>
    </div>
  </div>
  <div class="div1">
    <h1>Header inside div 1</h1>
    <p>
      Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
      one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
      Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
      in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied
      by English versions from the 1914 translation by H. Rackham.
    </p>
  </div>
  <div class="div2">
    <h1>header inside div 2</h1>
    <p>
      Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
      one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
      Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
      in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied
      by English versions from the 1914 translation by H. Rackham.
    </p>
  </div>
  <div class="div3">
    <h1>header in div 3</h1>
    <p>
      Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
      one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus
      Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line
      in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied
      by English versions from the 1914 translation by H. Rackham.
    </p>
  </div>

  <div class="clear"></div>

  <div class="footer">
    <h2 id="footer-h2">This is the footer</h2>
  </div>
</body>

</html>

CodePudding user response:

It is because of the default margin on the header h1 and h2 tag use below css

.header>h1 {
  margin-top: 0;
}

.footer>h2 {
  margin-top:0;
}

Also one suggestion, please use flexbox instead of float, as flexbox will handle responsive design as well

  • Related