DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>title>
<style type="text/css">
.box1 {
width: 1024px;
height: 724px;
margin: 0 auto;
/*设置背景样式*/
background-color: #bfa;
background-image: url(img/1.png);
background-repeat: repeat-y;
}
style>
head>
<body>
<div class="box1">div>
body>
html>

/*设置背景样式*/
background-color: #bfa;
background-image 来设置背景图片
background-image: url(img/1.png);
background-repeat: repeat-y;
DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.box1 {
height: 500px;
margin: 0 auto;
/*
* 设置一个背景
*/
background-color: #bfa;
/*
* 设置一个背景图片
*/
background-image: url(img/4.png);
/*
* 设置一个图片不重复
*/
background-repeat: no-repeat;
/* background-position: -80px -40px; */
background-attachment: fixed;
}
body {
background-image: url(img/3.png);
background-repeat: no-repeat;
background-attachment: fixed;
}
style>
head>
<body style="height: 5000px;">
<div class="box1">div>
body>
html>

也可以直接指定两个偏移量
第一个是水平偏移量
第二个是垂直偏移量
background-position: -80px -40px;
background-attachment 用来设置背景图片是否随着页面一起滚动
可选值: - scroll,默认值,背景图片随着窗口滚动 - fixed,背景图片会固定在某一位置,不随页面滚动
不随窗口滚动的图片,我们一般都设置给 body,而不设置给其他元素
当背景图片的 background-attachment 设置为 fixed 时,
background-attachment: fixed;

DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>title>
<style type="text/css">
body {
background-color: #bfa;
background: #bfa url(img/3.png) center center no-repeat fixed;
}
style>
head>
<body>body>
html>

/* 设置一个背景颜色 */
/background-color: #bfa;
/*设置一个背景图片 */
background-image: url(img/3.png);
/* 设置背景不重复 */
background-repeat: no-repeat;
/*设置背景图片的位置*/
background-position: center center;
/*设置背景图片不随滚动条滚动 */
background-attachment: fixed;
background: #bfa url(img/3.png) center center no-repeat fixed;