HTML背景颜色没有填满整个页面?

时间:2014-08-15 15:26:00

标签: html css

嘿,这里的每个人都是问题的图片:

enter image description here

我希望它填充绿色框左侧和顶部的空白区域。

HTML:

<!DOCTYPE HTML>
<html>
    <head>
        <LINK href="style.css" rel="stylesheet" type="text/css">
        <link href='http://fonts.googleapis.com/css?family=Montserrat:700' rel='stylesheet' type='text/css'>
        <title>Test page</title>
    </head>
    <body>
    <div id="container" name="header">
        <h1>Blegh</h1>
            <style>
                #container {font-family: 'Montserrat', sans-serif;}
            </style>
     </div>
    </body>
</html>

CSS:

#container{
    background-color: #58FA58;
    margin-left: auto;
    margin-right: auto;
    text-align: center;
    height: 100px;
    width: 100%;
}

6 个答案:

答案 0 :(得分:6)

这两条规则应该这样做:

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

<强> jsFiddle example

答案 1 :(得分:4)

这是因为浏览器。默认的浏览器有边距和填充,你应该在所有项目中将填充和边距设置为零

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

答案 2 :(得分:2)

将div高度设置为100 px:)

使div像boddy包装你应该有这个CSS

    * {
    margin: 0;
}

html, body {
    height: 100%;
}

#container{
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -20px;
}

.footer, .push {
    height: 20px;
}

答案 3 :(得分:0)

您必须删除身体边距。将此添加到您的CSS:

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

答案 4 :(得分:0)

将此添加到style.css:

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

答案 5 :(得分:0)

应用边距和填充为0将允许所有元素到达页面边框。但是,如果您希望颜色填充整个页面(这似乎是实际问题),请将背景颜色应用于正文标记。

body {
    margin: 0px;
    padding: 0px;
    background-color: #58FA58;
}