欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

css background-size属性怎么用

程序员文章站 2022-03-16 15:31:44
...
css background-size属性可以设置背景图片的大小。语法:background-size: length|percentage|cover|contain;通过长度值或百分比来设置图片大小,或者通过cover和contain来对图片进行伸缩设置。

css background-size属性怎么用

css background-size属性怎么用?

作用:规定背景图像的尺寸。

语法:

background-size: length|percentage|cover|contain;

说明:

length 设置背景图像的高度和宽度。第一个值设置宽度,第二个值设置高度。如果只设置一个值,则第二个值会被设置为 "auto"。percentage 以父元素的百分比来设置背景图像的宽度和高度。

第一个值设置宽度,第二个值设置高度。如果只设置一个值,则第二个值会被设置为 "auto"。cover 把背景图像扩展至足够大,以使背景图像完全覆盖背景区域。背景图像的某些部分也许无法显示在背景定位区域中。contain 把图像图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域。

注释:IE9+、Firefox 4+、Opera、Chrome 以及 Safari 5+ 支持 background-size 属性。

css background-size属性使用示例

<!DOCTYPE html>
<html>
<head>
<style> 
body
{
background:url('https://img.php.cn/upload/article/000/005/656/5af270fd37755429.jpg');
background-size:100px 63px;
-moz-background-size:100px 63px; /* 老版本的 Firefox */
background-repeat:no-repeat;
padding-top:80px;
}
</style>
</head>
<body>
<p>上面是缩小的背景图片。</p>
<p>原始图片:<img src="https://img.php.cn/upload/article/000/005/656/5af270fd37755429.jpg" alt="Flowers"></p>
</body>
</html>

效果输出:

css background-size属性怎么用

以上就是css background-size属性怎么用的详细内容,更多请关注其它相关文章!

相关标签: css background-size