背景图片html写法,CSS3背景图片background属性简写/连写

这篇博客详细介绍了CSS中background属性的各个子属性,包括background-color、background-image、background-position、background-size、background-repeat、background-attachment等,以及它们的使用方法和组合写法。重点讲解了如何设置背景颜色、图像、位置、尺寸和重复方式,帮助开发者更好地理解和应用CSS背景属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

e1312f64dced1376f9ddc572f5c3d1a7.png

在开发中背景属性 background 还是很常用的, background 有很多属性,如:

background-color 背景颜色

background-image 背景图像

background-position 背景图像的位置

background-size 背景图片的尺寸

background-repeat 重复背景图像

background-origin 定位区域

background-clip 绘制区域

background-attachment 是否固定或者跟随页面滚动。( scroll :默认值,随页面滚动;fixed 固定,不滚动;inherit继承。)

当然这些属性可以分开写,也可以连写(简写),规则(顺序)如下:background:color img_url repeat attachment position / size

颜色 图片地址 重复 是否固定 位置 / 尺寸

示例:background:#F00 url('img/images.png') no-repeat fixed center / cover

红色背景,不重复,固定背景,居中,短边填充。

图像位置和尺寸用 / 隔开,前面是 background-position ,后面是 background-size 。

其中 background-position 的写法注意:语义描述时是 top left ,数值描述时是 x-pos y-pos ,两种顺序相反。 background-size 的写法是:width height 。

这样分开写会更好理解一些:background:#F00 url('img/images.png') no-repeat fixed top left / 100% auto

靠左上,宽度 100% ,高度自适应。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值