前端学习之背景图错位

前端学习之背景图错位

现象

现象如下图:
亲刷新试试
这里的想法是不将背景图片放到body上,而是body充满屏幕,div(这个div作为整个内容容器)充满body,然后div背景图充满div。

分析

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>csstest</title>
<style>
html{
height:100%;
}
body{
background-color: red;
height: 100%;
margin: 0px;
padding: 0px;
overflow: hidden;
}

#container{
background: url(image/dota2Index.jpg);
background-size: cover;
background-repeat: no-repeat;
color: white;
height: 100%;
font-size: xx-large;
padding: 0px;
margin: 0px;
width: 100%;
/* position: absolute;
top: 0;
left: 0;*/
}

#container p{
text-align: center;
}
</style>

</head>
<body>
<div id="container">
<p id="welcomeWord"> welcome to dota2</p>
</div>
</body>
</html>

**解决方法

将稳重注释掉的地方恢复。