CSS 多重背景
- 上一页 CSS border image
- 下一页 CSS color
在本章中,您将学习如何将多个背景图像添加到一个元素。
您还将学到以下属性:
background-size
background-origin
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; }
多重背景图像可以使用单独的背景属性(如上所述)或 background
简写属性来指定。
下面的例子使用 background
简写属性(结果与上例相同):
实例
#example1 { background: url(flower.gif) right bottom no-repeat, url(paper.gif) left top repeat; }
kwarin tarin shida na CSS
CSS background-size
ka dace amfani da tarin shida a hankali don kiyaye tarin shida.
ka dace amfani da kudu, kiyasi ko amfani da kiyaye daga cikin hakan:contain
tana da wata: cover
.
tarihinin da ke keɗa yaɗa shida, akwai tarin shida da yawa (a cikin pixel):
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.
wannan shi ne kudin code:
实例
#div1 { background: url(img_flower.jpg); background-size: 100px 80px; background-repeat: no-repeat; }
background-size
ka dace sabon koyarwa na biyu akwai: contain
da cover
.
contain
ka dace tarin shida a hankali domin yaɗa da yankin a hankali a kai tsaye (karfin da tsayin su su yiwa yankin alama). Idan a hankali, ko'ina yankin tarin shida zai zama ba a hankali ba a yankin rarrabu tarin shida.
cover
ka dace tarin shida a hankali don yaɗa da yankin alama a cikin tarin shida (karfin da tsayin su su yiwa yankin alama). Idan a hankali, ko'ina yankin tarin shida zai zama ba a hankali ba a yankin rarrabu tarin shida.
下面的例子展示了 contain
da cover
shaidar da amfani da su:
实例
#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; }
karantawa daga tarin shida na yawa
a cikin yin amfani da tarin shida da yawa:background-size
kuma akwai sabon koyarwa don rarrabu tarin shida (sauye da tsammanin gajerun saukar).
tarihinin da keɗa yaɗa shida, akwai tarin shida, tarin yamma kudu shida:
实例
#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; }
全尺寸背景图像
现在,我们希望网站上的背景图像始终覆盖整个浏览器窗口。
具体要求如下:
- 用图像填充整个页面(无空白)
- 根据需要缩放图像
- 在页面上居中图像
- 不引发滚动条
下面的例子显示了如何实现它:使用 <html> 元素(<html> 元素始终至少是浏览器窗口的高度)。然后在其上设置固定且居中的背景。最后使用 background-size 属性调整其大小:
实例
html { background: url(img_man.jpg) no-repeat center fixed; background-size: cover; }
Hero Image
您还可以在 <div> 上使用不同的背景属性来创建 Hero Image(带有文本的大图像),并将其放置在您希望的位置上。
实例
.hero-image { background: url(img_man.jpg) no-repeat center; background-size: cover; height: 500px; position: relative; }
CSS ƙanwar
CSS background-origin
属性指定背景图像的位置。
该属性接受三个不同的值:
- border-box - 背景图片从边框的左上角开始
- padding-box -背景图像从内边距边缘的左上角开始(默认)
- content-box - 背景图片从内容的左上角开始
下面的示例展示了 background-origin
属性:
实例
#example1 { border: 10px solid black; padding: 35px; background: url(flower.gif); background-repeat: no-repeat; background-origin: content-box; }
CSS ƙanwar
CSS background-clip
属性指定背景的绘制区域。
该属性接受三个不同的值:
- border-box - 背景绘制到边框的外部边缘(默认)
- padding-box - 背景绘制到内边距的外边缘
- content-box - 在内容框中绘制背景
下面的例子展示了 background-clip
属性:
实例
#example1 { border: 10px dotted black; padding: 35px; background: yellow; background-clip: content-box; }
CSS 高级背景属性
属性 | 描述 |
---|---|
background | 用于在一条声明中设置所有背景属性的简写属性。 |
background-clip | 规定背景的绘制区域。 |
background-image | 为一个元素指定一幅或多幅背景图像。 |
background-origin | 规定背景图像的放置位置。 |
background-size | 规定背景图像的大小。 |
- 上一页 CSS border image
- 下一页 CSS color