简体   繁体   English

设置元素HTML的背景样式

[英]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. 我正在建立一个网站,并且正在尝试一些新的东西,除了当我尝试为我的H1元素创建背景时,我无法确保背景颜色边框适合屏幕顶部,左侧和右侧。

<!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: 您需要将CSS更改为此:

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 的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. 您需要确保将身体设置为没有填充或边距,并且还必须确保h1也没有任何边距。 That way it will happily sit at the top of your page. 这样,它将很高兴地位于页面顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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