简体   繁体   中英

How do I get rid of the white space that comes around the border in CSS?

I was just messing around with CSS & HTML and added a header with a background color to the top of my website. Then I saw that there was lots of white space on top of the header's background color and to the sides of it. How do I get rid of this white space using CSS? Here is my code along with a picture of how the website looks right now:

<!DOCTYPE html>
<html>

<head>
<title>
Website
</title>
<link rel='stylesheet' href='style.css'/>
  <script src='script.js'></script>
  <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Baloo+Bhaina" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">

  <style>
    .typeofvirus{
    font-family: 'Open Sans', sans-serif;
    }
      br {
    line-height: 2%;
 }

    #topheader{
     background-color: #2c3e50;
 width: 100%;
 color: white;
 margin-top: -1%;

       }
    html, body {
margin: 0px;
padding: 0px;
}
  </style>
</head>
  <body>

    <p></p>
       <section id = "topheader">
       <div id = "topheaderdiv">
    <h1 style = "font-family: 'Baloo Bhaina', cursive; text-align: center;"><big>Hello</big><br><small><small><small style = "font-family: 'Nunito', sans-serif;">This is a webpage</small></small></small></h1>
    </div>
    </section>
<h2 class = "typeofvirus">What are Microbes?</h2>    
<p></p>
 <h3 class = "typeofvirus"><big>Types of viruses caused by microbes in the bathroom</big></h3>
<ul>
<li style = "font-family: 'Open Sans', sans-serif;"><i><big>Dermatophitic fungi</big></i>: Caused by people walking barefoot in bathrooms</li>
<li style = "font-family: 'Open Sans', sans-serif;"><i><big>Gastrointestinal viruses</big></i>: Caused by contact with a toilet. This can remain on a solid surface for over a week. This causes stomach ailments.</li>
<li style = "font-family: 'Open Sans', sans-serif;"><i><big>Residual fungi</big></i>: which can cause asthma or allergies. This is caused by the mold in the bathroom due to lack of cleaning.</li>
</ul>
 </body>

   </html>

图片显示了我的代码中发生了什么

You need to reset your body and html margin and padding too:

Add this to your CSS:

html, body {
    margin: 0px;
    padding: 0px;
}

I would go with anned20's answer but for the sake of academic curiosity you can also add

overflow: hidden;

to body and html. Or manually give the element a negative upper margin and a 100% width like this

#topheader{
  background-color: #2c3e50;
  width: 100%;
  color: white;
  margin-top: -1%;
} 

But, again, anned's solution is ideal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM