Häufig verwendete Hintergrundattribute sind: 1. Hintergrundfarbe; 3. Hintergrundwiederholung; 5. Hintergrundgröße;
Die Betriebsumgebung dieses Tutorials: Windows7-System, CSS3- und HTML5-Version, Dell G3-Computer.
Verwenden Sie die Hintergrundfarbe für das Element mit einem bestimmten Farbnamen (z. B. Rot) festlegen. | |
---|---|
transparent | |
inherit | |
[Beispiel] Verwenden Sie „background-color“, um die Hintergrundfarbe für das Element festzulegen: | |
<!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> #bg { color: white; background-color: blue; margin: 20px; /*设置外边距为 20px*/ padding: 20px; /*设置内边距为 20px*/ border: 10px dotted yellow; /*设置一个宽 10px 的黄色虚线边框*/ } </style> </head> <body> <p id="bg">background-color 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> #bg { color: white; background-color: blue; margin: 20px; /*设置外边距为 20px*/ padding: 20px; /*设置内边距为 20px*/ border: 10px dotted yellow; /*设置一个宽 10px 的黄色虚线边框*/ } </style> </head> <body> <p id="bg">background-color 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren | |
Bild: Demonstration des Attributs „Hintergrundfarbe“ |
【示例】使用 background-image 属性将图片【 <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> #bg { color: red; background-image: url('./bg-image.png'); margin: 20px; /*设置外边距为 20px*/ padding: 20px; /*设置内边距为 20px*/ border: 10px dotted red; /*设置一个宽 10px 的红色虚线边框*/ } </style> </head> <body> <p id="bg">background-image 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> #bg { color: red; background-image: url('./bg-image.png'); margin: 20px; /*设置外边距为 20px*/ padding: 20px; /*设置内边距为 20px*/ border: 10px dotted red; /*设置一个宽 10px 的红色虚线边框*/ } </style> </head> <body> <p id="bg">background-image 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren 图:background-image 属性演示 背景图像的覆盖区域与背景颜色相同,同样会填充元素的内容、内边距以及边框区域,对于元素边框以外的区域(外边距)则没有影响。 3. background-repeat默认情况下背景图像会从元素内容的左上角开始(若有内边距则从元素内边距区域的左上角开始),在水平和垂直方向上重复背景图像以填充整个元素区域(不包括元素的外边距区域),您可以使用 background-repeat 属性用来设置背景图像是否重复或如何重复,该属性的可选值如下:
【示例】使用 background-repeat 属性让背景图像只在水平方向上重复: <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> #bg { color: black; background-image: url('./bg-image.png'); background-repeat: repeat-x; margin: 20px; /*设置外边距为 20px*/ padding: 20px; /*设置内边距为 20px*/ border: 10px dotted red; /*设置一个宽 10px 的红色虚线边框*/ } </style> </head> <body> <p id="bg">background-repeat 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> #bg { color: black; background-image: url('./bg-image.png'); background-repeat: repeat-x; margin: 20px; /*设置外边距为 20px*/ padding: 20px; /*设置内边距为 20px*/ border: 10px dotted red; /*设置一个宽 10px 的红色虚线边框*/ } </style> </head> <body> <p id="bg">background-repeat 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren ![]() 图:background-repeat 属性演示 4. background-positionbackground-position 属性用来设置背景图像的起始位置,该属性的可选值如下:
【示例】使用 background-position 属性来设置背景图像的位置: <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> #bg { color: black; background-image: url('./bg-image.png'); background-repeat: no-repeat; background-position: 0% 50%; margin: 20px; /*设置外边距为 20px*/ padding: 20px; /*设置内边距为 20px*/ border: 10px dotted red; /*设置一个宽 10px 的红色虚线边框*/ } </style> </head> <body> <p id="bg">background-position 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> #bg { color: black; background-image: url('./bg-image.png'); background-repeat: no-repeat; background-position: 0% 50%; margin: 20px; /*设置外边距为 20px*/ padding: 20px; /*设置内边距为 20px*/ border: 10px dotted red; /*设置一个宽 10px 的红色虚线边框*/ } </style> </head> <body> <p id="bg">background-position 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren 图:background-position 属性演示 5. background-sizebackground-size 属性用来设置背景图像的尺寸,该属性的可选值如下:
【示例】使用 background-size 属性设置背景图像的尺寸,并将背景图像横向铺满整个元素区域: <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> body { background-image: url('./bg-image.png'); background-repeat: repeat-x; background-size: contain; } </style> </head> <body> <p>background-size 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> body { background-image: url('./bg-image.png'); background-repeat: repeat-x; background-size: contain; } </style> </head> <body> <p>background-size 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren 图:background-size 属性演示 6. backgroundbackground 是背景属性的简写形式,通过它不仅可以为元素设置某个背景属性,还可以同时设置多个或者所有的背景属性。在设置多个背景属性时并没有固定的顺序,但推荐使用如下顺序进行设置:background-color || background-image || background-position [/ background-size]? || background-repeat || background-attachment || background-origin || background-clip Nach dem Login kopieren
【示例】通过 background 同时设置多个背景属性: <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> #bg { background: #ccc url('./bg-image.png') 0px 0px/contain repeat-x border-box; margin: 20px; /*设置外边距为 20px*/ padding: 20px; /*设置内边距为 20px*/ border: 10px dotted red; /*设置一个宽 10px 的红色虚线边框*/ } </style> </head> <body> <p id="bg">background 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> #bg { background: #ccc url('./bg-image.png') 0px 0px/contain repeat-x border-box; margin: 20px; /*设置外边距为 20px*/ padding: 20px; /*设置内边距为 20px*/ border: 10px dotted red; /*设置一个宽 10px 的红色虚线边框*/ } </style> </head> <body> <p id="bg">background 属性</p> </body> </html> Nach dem Login kopieren Nach dem Login kopieren 图:background 属性演示 background 属性还支持设置多组属性值(比如上面示例中的 <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> body { background: url("./css.png") 10px 10px/60px 60px no-repeat padding-box, url("./css.png") 50px 30px/120px 120px no-repeat content-box, url("./css.png") 140px 40px/200px 100px no-repeat content-box #58a; } </style> </head> <body> </body> </html> Nach dem Login kopieren Nach dem Login kopieren <!DOCTYPE html> <html> <head> <title>CSS背景</title> <style> body { background: url("./css.png") 10px 10px/60px 60px no-repeat padding-box, url("./css.png") 50px 30px/120px 120px no-repeat content-box, url("./css.png") 140px 40px/200px 100px no-repeat content-box #58a; } </style> </head> <body> </body> </html> Nach dem Login kopieren Nach dem Login kopieren 图:多重背景层叠效果 (学习视频分享:css视频教程) |
---|
Das obige ist der detaillierte Inhalt vonWas sind die häufig verwendeten Hintergrundattribute in CSS3?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!