小码哥的IT人生

CSS 多重背景 详解

css3基础 2022-05-23 12:07:06小码哥的IT人生shichen

CSS 多重背景

在本章中,您将学习如何将多个背景图像添加到一个元素。

您还将学到以下属性:

  1. background-size
  2. background-origin
  3. background-clip

CSS 多重背景

CSS 允许您通过 background-image 属性为一个元素添加多幅背景图像。

不同的背景图像用逗号隔开,并且图像会彼此堆叠,其中的第一幅图像最靠近观看者。

下面的例子有两幅背景图像,第一幅图像是花朵(与底部和右侧对齐),第二幅图像是纸张背景(与左上角对齐):

示例代码:

#example1 {
  background-image: url(flower.gif), url(paper.gif);
  background-position: right bottom, left top;
  background-repeat: no-repeat, repeat;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
  background-image: url(/i/photo/flower.gif), url(/i/paper.jpg);
  background-position: right bottom, left top;
  background-repeat: no-repeat, repeat;
  padding: 15px;
}
</style>
</head>
<body>
<h1>多重背景</h1>
<p>下面的 div 元素有两副背景图像:</p>
<div id="example1">
  <h1>Welcome to Shanghai</h1>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
  <p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

多重背景图像可以使用单独的背景属性(如上所述)或 background 简写属性来指定。

下面的例子使用 background 简写属性(结果与上例相同):

示例代码:

#example1 {
  background: url(flower.gif) right bottom no-repeat, url(paper.gif) left top repeat;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
  background: url(/i/photo/flower.gif) right bottom no-repeat, url(/i/paper.jpg) left top repeat;
  padding: 15px;
}
</style>
</head>
<body>
<div id="example1">
  <h1>Welcome to Shanghai</h1>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
  <p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

CSS 背景尺寸

CSS background-size 属性允许您指定背景图像的大小。

可以通过长度、百分比或使用以下两个关键字之一来指定背景图像的大小:containcover

下面的例子将背景图像的大小调整为比原始图像小得多(使用像素):

Lorem Ipsum Dolor

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

这是代码:

示例代码:

#div1 {
  background: url(img_flower.jpg);
  background-size: 100px 80px;
  background-repeat: no-repeat;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
  border: 1px solid black;
  background: url(/i/photo/flower.gif);
  background-size: 100px 80px;
  background-repeat: no-repeat;
  padding: 15px;
}
#example2 {
  border: 1px solid black;
  background: url(/i/photo/flower.gif);
  background-repeat: no-repeat;
  padding: 15px;
}
</style>
</head>
<body>
<h1>background-size 属性</h1>
<p>被调整大小的 background-image:</p>
<div id="example1">
  <h2>Welcome to Shanghai</h2>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
  <p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>
<p>background-image 的原始尺寸:</p>
<div id="example2">
  <h2>Welcome to Shanghai</h2>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
  <p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

background-size 的其他两个可能的值是 containcover

contain 关键字将背景图像缩放为尽可能大的尺寸(但其宽度和高度都必须适合内容区域)。这样,取决于背景图像和背景定位区域的比例,可能存在一些未被背景图像覆盖的背景区域。

cover 关键字会缩放背景图像,以使内容区域完全被背景图像覆盖(其宽度和高度均等于或超过内容区域)。这样,背景图像的某些部分可能在背景定位区域中不可见。

下面的例子展示了 containcover 的用法:

示例代码:

#div1 {
  background: url(img_flower.jpg);
  background-size: contain;
  background-repeat: no-repeat;
}
#div2 {
  background: url(img_flower.jpg);
  background-size: cover;
  background-repeat: no-repeat;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
.div1 {
  border: 1px solid black;
  height: 120px;
  width: 150px;
  background: url(/i/photo/flower.gif);
  background-repeat: no-repeat;
  background-size: contain;
}
.div2 {
  border: 1px solid black;
  height: 120px;
  width: 150px;
  background: url(/i/photo/flower.gif);
  background-repeat: no-repeat;
  background-size: cover;
}
.div3 {
  border: 1px solid black;
  height: 120px;
  width: 150px;
  background: url(/i/photo/flower.gif);
  background-repeat: no-repeat;
}
</style>
</head>
<body>
<h1>background-size 属性</h1>
<h2>background-size: contain:</h2>
<div class="div1">
<p>Lorem ipsum dolor sit amet.</p>
</div>
<h2>background-size: cover:</h2>
<div class="div2">
<p>Lorem ipsum dolor sit amet.</p>
</div>
<h2>background-size 未定义:</h2>
<div class="div3">
<p>Lorem ipsum dolor sit amet.</p>
</div>
<p>原始图像:</p>
<img src="/i/photo/flower.gif" alt="Flowers" width="224" height="162">
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

定义多个背景图像的尺寸

在处理多重背景时,background-size 属性还可以接受多个设置背景尺寸的值(使用逗号分隔的列表)。

下面的例子指定了三幅背景图像,每幅图像有不同的 background-size 值:

示例代码:

#example1 {
  background: url(tree.png) left top no-repeat, url(flower.gif) right bottom no-repeat,
  	url(paper.gif) left top repeat;
  background-size: 50px, 130px, auto;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
  background: url(/i/photo/tree.png) left top no-repeat, url(/i/photo/flower.gif) right bottom no-repeat, url(/i/paper.jpg) left top repeat;
  padding: 15px;
  background-size: 50px, 130px, auto;
}
</style>
</head>
<body>
<div id="example1">
  <h1>Welcome to Shanghai</h1>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
  <p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

全尺寸背景图像

现在,我们希望网站上的背景图像始终覆盖整个浏览器窗口。

具体要求如下:

  1. 用图像填充整个页面(无空白)
  2. 根据需要缩放图像
  3. 在页面上居中图像
  4. 不引发滚动条

下面的例子显示了如何实现它:使用 <html> 元素(<html> 元素始终至少是浏览器窗口的高度)。然后在其上设置固定且居中的背景。最后使用 background-size 属性调整其大小:

示例代码:

html {
  background: url(img_man.jpg) no-repeat center fixed;
  background-size: cover;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
html {
  background: url(/i/photo/tiyugongyuan.jpg) no-repeat center fixed;
  background-size: cover;
}
body {
  color: white;
}
</style>
</head>
<body>
<h1>完整页面的背景图像</h1>
<p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai! The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

Hero Image

您还可以在 <div> 上使用不同的背景属性来创建 Hero Image(带有文本的大图像),并将其放置在您希望的位置上。

示例代码:

.hero-image {
  background: url(img_man.jpg) no-repeat center;
  background-size: cover;
  height: 500px;
  position: relative;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}
.hero-image {
  background: url(/i/photo/tiyugongyuan.jpg) no-repeat center;
  background-size: cover;
  height: 500px;
  position: relative;
}
.hero-text {
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
}
</style>
</head>
<body>
<div class="hero-image">
  <div class="hero-text">
    <h1 style="font-size:50px">我是比尔盖茨</h1>
    <h3>我是一位摄影师</h3>
    <button>聘请我吧</button>
  </div>
</div>
<p>页面内容..</p>
<p>请注意,这项技术会将使图像响应:请调整浏览器窗口的大小来查看效果。</p>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

CSS background-origin 属性

CSS background-origin 属性指定背景图像的位置。

该属性接受三个不同的值:

  1. border-box - 背景图片从边框的左上角开始
  2. padding-box -背景图像从内边距边缘的左上角开始(默认)
  3. content-box - 背景图片从内容的左上角开始

下面的示例展示了 background-origin 属性:

示例代码:

#example1 {
  border: 10px solid black;
  padding: 35px;
  background: url(flower.gif);
  background-repeat: no-repeat;
  background-origin: content-box;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
  border: 10px solid black;
  padding: 35px;
  background: url(/i/photo/flower.gif);
  background-repeat: no-repeat;
}
#example2 {
  border: 10px solid black;
  padding: 35px;
  background: url(/i/photo/flower.gif);
  background-repeat: no-repeat;
  background-origin: border-box;
}
#example3 {
  border: 10px solid black;
  padding: 35px;
  background: url(/i/photo/flower.gif);
  background-repeat: no-repeat;
  background-origin: content-box;
}
</style>
</head>
<body>
<h1>background-origin 属性</h1>
<p>未设置 background-origin (padding-box 为默认):</p>
<div id="example1">
  <h2>Welcome to Shanghai</h2>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
  <p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>
<p>background-origin: border-box:</p>
<div id="example2">
  <h2>Welcome to Shanghai</h2>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
  <p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>
<p>background-origin: content-box:</p>
<div id="example3">
  <h2>Welcome to Shanghai</h2>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
  <p>The city is located on the southern estuary of the Yangtze, with the Huangpu River flowing through it.</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

CSS background-clip 属性

CSS background-clip 属性指定背景的绘制区域。

该属性接受三个不同的值:

  1. border-box - 背景绘制到边框的外部边缘(默认)
  2. padding-box - 背景绘制到内边距的外边缘
  3. content-box - 在内容框中绘制背景

下面的例子展示了 background-clip 属性:

示例代码:

#example1 {
  border: 10px dotted black;
  padding: 35px;
  background: yellow;
  background-clip: content-box;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
#example1 {
  border: 10px dotted black;
  padding: 35px;
  background: yellow;
}
#example2 {
  border: 10px dotted black;
  padding: 35px;
  background: yellow;
  background-clip: padding-box;
}
#example3 {
  border: 10px dotted black;
  padding: 35px;
  background: yellow;
  background-clip: content-box;
}
</style>
</head>
<body>
<h1>background-clip 属性</h1>
<p>No background-clip (border-box is default):</p>
<div id="example1">
  <h2>Welcome to Shanghai</h2>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
</div>
<p>background-clip: padding-box:</p>
<div id="example2">
  <h2>Welcome to Shanghai</h2>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
</div>
<p>background-clip: content-box:</p>
<div id="example3">
  <h2>Welcome to Shanghai</h2>
  <p>Shanghai is one of the four direct-administered municipalities of the People's Republic of China. Welcome to Shanghai!</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

CSS 高级背景属性

属性 描述
background 用于在一条声明中设置所有背景属性的简写属性。
background-clip 规定背景的绘制区域。
background-image 为一个元素指定一幅或多幅背景图像。
background-origin 规定背景图像的放置位置。
background-size 规定背景图像的大小。

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024