ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## background-size 属性 background-size属性指定背景图片大小。 JavaScript 语法:object&nbsp;object.style.backgroundSize="60px 80px" * * * ## 语法 ``` background-size: *length*|*percentage*|cover|contain; ``` | 值 | 描述 | | :-- | :-- | | length | 设置背景图片高度和宽度。第一个值设置宽度,第二个值设置的高度。如果只给出一个值,第二个是设置为"auto(自动)" | | percentage | 将计算相对于背景定位区域的百分比。第一个值设置宽度,第二个值设置的高度。如果只给出一个值,第二个是设置为"auto(自动)" | | cover | 此时会保持图像的纵横比并将图像缩放成将完全覆盖背景定位区域的最小大小。 | | contain | 此时会保持图像的纵横比并将图像缩放成将适合背景定位区域的最大大小。 | * * * ## 在线实例--[拉伸背景图像](https://www.w3cschool.cn/tryrun/showhtml/trycss3_background-size2) ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>W3Cschool教程(w3cschool.cn)</title> <style> div { background:url(/statics/images/course/img_flwr.gif); background-size:100% 100%; background-repeat:no-repeat; } </style> </head> <body> <div> 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. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. </div> </body> </html> ``` ## [四个背景图像图像横向拉伸](https://www.w3cschool.cn/tryrun/showhtml/trycss3_background-size3) ``` <!DOCTYPE html> <html> <head> <style> div { background:url(/images/w3css.gif); background-size:25%; border:2px solid #92b901; } </style> </head> <body> <div> 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. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. </div> </body> </html> ```