简体   繁体   中英

Styling the background of an Element HTML

I am making a website and I am trying something new, except when I try to create a background for my H1 element I have no way of making sure the background color borders fit to the top of the screen, left and right.

<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" type="text/css" href="site.css"
<body>
<title>Test Tab</title>
<div class="main">
    <h1>Website Heading</h1>
</div>

</body>
</html>

.main {
 font-family: Arial, Verdana, sans-serif;
 text-align: center;
 font-size: 20px;
 background-color: #00ff00;
 background-position: 10px;
}

You need to change your CSS to this:

html,body {
  margin:0;
  padding:0;
}
.main {
 font-family: Arial, Verdana, sans-serif;
 text-align: center;
 font-size: 20px;
 background-color: #00ff00;
 background-position: 10px;
}

.main h1 {
  margin:0;
}

JSFiddle

You need to make sure you set your body to have no padding or margins and also make sure your h1 doesn't have any margin either. That way it will happily sit at the top of your page.

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