什么是 background?
在 CSS(层叠样式表)中,background 是一个用于设置 HTML 元素背景样式的复合属性。
它可以控制背景颜色、背景图片、背景重复方式、背景位置、背景尺寸等。
background 的常用子属性
background-color:设置背景颜色。background-image:设置背景图像。background-repeat:设置背景图像是否及如何重复。background-position:设置背景图像的起始位置。background-size:设置背景图像的尺寸。background-attachment:设置背景图像是否随页面滚动。
简写语法示例
你可以使用简写形式一次性设置多个背景属性:
background: #ffffff url("bg.jpg") no-repeat center/cover fixed;
上述代码等价于:
background-color: #ffffff;
background-image: url("bg.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-attachment: fixed;
应用场景
background 广泛应用于网页设计中,例如:
设置页面整体背景、卡片组件的装饰性底图、按钮悬停效果等。
合理使用 background 可提升用户体验与视觉美感。