如何使用一张图片当作背景呢???
首先先在body标签里面随便写一个双列标签,比如下面的div标签:
<body>
<div></div>
</body>
然后创建一个style标签,写CSS代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 120px;
height: 60px;
border: 5px solid crimson;
padding: 300px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
上面的style标签中的div中写的创建了一个块
margin: 10px auto;
上面代码将块在网页中居中显示,然后开始在这个块中插入背景图片:
插入的方法就是引入一个新属性:
<style>
div{
width: 120px;
height: 60px;
border: 5px solid crimson;
padding: 300px;
margin: 10px auto;
background-image: url(images/dilireba.jpg);
}
</style>
上面的background-image:url()属性是将图片通过路径引进来
但是这样的背景是不是不尽人意呢?
<style>
div{
width: 120px;
height: 60px;
border: 5px solid crimson;
padding: 300px;
margin: 10px auto;
background-image: url(images/dilireba.jpg);
background-repeat: no-repeat;
}
</style>
发现上面的背景图片按照实际大小显示在了块中的左上角,因为background-repeat: no-repeat;的属性就是让背景图片不进行延伸(延展)。
提到这里再介绍两个相似的属性:
background-repeat: repeat-x;
上面的是沿着x轴的方向进行延伸。同理:
background-repeat: repeat-y;
上面的是沿着y轴进行延展的。
继续向下进行:
<style>
div{
width: 120px;
height: 60px;
border: 5px solid crimson;
padding: 300px;
margin: 10px auto;
background-image: url(images/dilireba.jpg);
background-repeat: no-repeat;
background-attachment:scroll;
}
</style>
注意:
上面的 background-attachment:scroll;属性是保证在上下滑动网页的同时,背景图片也同时上下滑动,而如果换成background-attachment:fixed;属性,那么背景图片是固定在网页上不变的。(可以自行调试,这里不做演示)。
对于上面的背景图片,设置的依然不够美观,那么能不能将背景图片居中在块中呢?
<style>
div{
width: 120px;
height: 60px;
border: 5px solid crimson;
padding: 300px;
margin: 10px auto;
background-image: url(images/dilireba.jpg);
background-repeat: no-repeat;
background-attachment:scroll;
background-position: center;
}
</style>
上面的background-position: center;属性就是将其居中,当然,我们不一定非要将图片居中,可以自己设置图片所在的区域范围:
<style>
div{
width: 120px;
height: 60px;
border: 5px solid crimson;
padding: 300px;
margin: 10px auto;
background-image: url(images/dilireba.jpg);
background-repeat: no-repeat;
background-attachment:scroll;
/* background-position: center; */
background-position: 100px 80px;
}
</style>
如上面的自己进行设置距离左边距与上边距的像素距离。
对于上面的多个属性,可以合一进行使用:
background: url(images/dilireba.jpg) no-repeat scroll 200px 130px;
上面的等价于一条一条的写出,注意书写的顺序
综上代码展示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width: 120px;
height: 60px;
border: 5px solid crimson;
padding: 300px;
margin: 10px auto;
background-image: url(images/dilireba.jpg);
background-repeat: no-repeat;
/* background-repeat: repeat-x; */
/* background-repeat: repeat-y; */
background-attachment: scroll;
/* background-position: center; */
background-position: 100px 130px;
/* background: url(images/dilireba.jpg) no-repeat scroll 200px 130px; */
}
</style>
</head>
<body>
<div>迪丽热巴</div>
</body>
</html>
上面方法的举例:
给出下面的一张图片:
如何将上面的图片转变成下面的样式:
方案1:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 88px;
height: 100px;
/* border: solid springgreen; */
background-image: url(images/peach.png);
float: left;
}
span {
width: 88px;
height: 100px;
/* border: solid springgreen; */
background-image: url(images/peach.png);
background-repeat: repeat-x;
margin: 30px;
/* margin-left: 100px; */
/* margin-top: 2px; */
float: left;
}
</style>
</head>
<body>
<div></div>
<span></span>
</body>
</html>
方案2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 88px;
height: 100px;
/* border: solid springgreen; */
background-image: url(images/peach.png);
float: left;
}
span {
width: 88px;
height: 100px;
/* border: solid springgreen; */
background-image: url(images/peach.png);
/* background-repeat: repeat-x; */
/* margin: 30px; */
margin-left: 5px;
margin-top: 40px;
float: left;
}
</style>
</head>
<body>
<!-- <img src="images/peach.png"> -->
<div></div>
<span></span>
</body>
</html>
说明:上面的两种方法,都是使用了两个块,做成两个背景,使用float属性进行漂浮
补充:
多层背景:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多层背景</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 1220px;
height: 1205px;
border: 5px dotted pink;
background-repeat: no-repeat;
display: inline-block;
}
.bg{
background-image: url(images/dabai.jpg) , url(images/dilireba.jpg);
}
.bg2{
background-image: url(images/dilireba.jpg) , url(images/dabai.jpg);
}
</style>
</head>
<body>
<p>两张背景图,第一张透明,第二张不透明</p>
<img src="images/dabai.jpg" alt="大白">
<img src="images/dilireba.jpg" alt="迪丽热巴">
<div class="bg"></div>
<div class="bg2"></div>
</body>
</html>