背景属性用于控制元素的背景外观,包括背景颜色、背景图片、背景位置等。掌握背景属性对于创建美观的网页界面至关重要。
background-color 属性用于设置元素的背景颜色。
/* 使用颜色名称 */
.box {
background-color: red;
}
/* 使用十六进制 */
.header {
background-color: #333333;
}
/* 使用 RGB */
.section {
background-color: rgb(51, 51, 51);
}
/* 使用 RGBA(带透明度) */
.overlay {
background-color: rgba(0, 0, 0, 0.5);
}
/* 使用 HSL */
.container {
background-color: hsl(0, 0%, 95%);
}
/* 使用 HSLA(带透明度) */
.background {
background-color: hsla(0, 0%, 95%, 0.9);
}
/* 透明背景 */
.transparent {
background-color: transparent;
}
/* 页面背景 */
body {
background-color: #f5f5f5;
}
/* 导航栏背景 */
.nav {
background-color: #2c3e50;
color: white;
}
/* 卡片背景 */
.card {
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* 按钮背景 */
.button {
background-color: #007bff;
color: white;
}
.button:hover {
background-color: #0056b3;
}
background-image 属性用于设置元素的背景图片。
/* 使用 URL 引入图片 */
.box {
background-image: url('background.jpg');
}
/* 使用相对路径 */
.container {
background-image: url('../images/bg.png');
}
/* 使用绝对路径 */
.header {
background-image: url('/assets/images/header-bg.jpg');
}
/* 使用网络图片 */
.hero {
background-image: url('https://example.com/image.jpg');
}
/* 使用渐变 */
.gradient {
background-image: linear-gradient(to right, red, blue);
}
/* 无背景图片 */
.no-bg {
background-image: none;
}
/* 线性渐变 */
.linear-gradient {
background-image: linear-gradient(to right, #3498db, #9b59b6);
}
/* 多色渐变 */
.multi-gradient {
background-image: linear-gradient(to right, red, yellow, green, blue, purple);
}
/* 对角渐变 */
.diagonal-gradient {
background-image: linear-gradient(45deg, #3498db, #9b59b6);
}
/* 径向渐变 */
.radial-gradient {
background-image: radial-gradient(circle, #3498db, #9b59b6);
}
/* 重复渐变 */
.repeating-gradient {
background-image: repeating-linear-gradient(45deg, #3498db, #3498db 10px, #9b59b6 10px, #9b59b6 20px);
}
background-repeat 属性用于设置背景图片的重复方式。
/* 水平和垂直重复(默认) */
.box {
background-repeat: repeat;
}
/* 不重复 */
.no-repeat {
background-repeat: no-repeat;
}
/* 水平重复 */
.repeat-x {
background-repeat: repeat-x;
}
/* 垂直重复 */
.repeat-y {
background-repeat: repeat-y;
}
/* 空间重复 */
.space {
background-repeat: space;
}
/* 圆整重复 */
.round {
background-repeat: round;
}
/* 水平分隔线 */
.divider {
height: 2px;
background-image: url('divider.png');
background-repeat: repeat-x;
}
/* 垂直分隔线 */
.vertical-divider {
width: 2px;
background-image: url('divider.png');
background-repeat: repeat-y;
}
/* 不重复的背景图片 */
.hero {
background-image: url('hero-bg.jpg');
background-repeat: no-repeat;
background-size: cover;
}
background-position 属性用于设置背景图片的位置。
/* 使用关键字 */
.top-left {
background-position: top left;
}
.center {
background-position: center;
}
.bottom-right {
background-position: bottom right;
}
/* 使用百分比 */
.center-center {
background-position: 50% 50%;
}
/* 使用像素 */
.pixel {
background-position: 10px 20px;
}
/* 混合使用 */
.mixed {
background-position: center 10px;
}
/* 0% 0% - 左上角 */
.box {
background-position: 0% 0%;
}
/* 50% 50% - 居中 */
.box {
background-position: 50% 50%;
}
/* 100% 100% - 右下角 */
.box {
background-position: 100% 100%;
}
/* 水平居中,垂直顶部 */
.box {
background-position: 50% 0%;
}
/* 图标定位 */
.icon {
background-image: url('icons.png');
background-repeat: no-repeat;
}
.icon-home {
background-position: 0 0;
}
.icon-user {
background-position: -32px 0;
}
.icon-settings {
background-position: -64px 0;
}
/* 精灵图 */
.sprite {
background-image: url('sprite.png');
background-repeat: no-repeat;
}
.sprite-1 {
background-position: 0 0;
}
.sprite-2 {
background-position: -50px 0;
}
.sprite-3 {
background-position: 0 -50px;
}
background-size 属性用于设置背景图片的大小。
/* 自动大小(默认) */
.auto {
background-size: auto;
}
/* 覆盖整个元素 */
.cover {
background-size: cover;
}
/* 包含整个图片 */
.contain {
background-size: contain;
}
/* 使用像素 */
.pixel {
background-size: 200px 100px;
}
/* 使用百分比 */
.percent {
background-size: 50% 50%;
}
/* 只设置宽度,高度自动 */
.width-only {
background-size: 200px;
}
/* cover - 背景图片覆盖整个元素,可能会裁剪图片 */
.hero {
background-image: url('hero.jpg');
background-size: cover;
background-position: center;
}
/* contain - 背景图片完全显示,可能会有空白 */
.card {
background-image: url('card-bg.jpg');
background-size: contain;
background-position: center;
background-repeat: no-repeat;
}
/* 全屏背景 */
.fullscreen {
background-image: url('fullscreen-bg.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
/* 图标背景 */
.icon {
background-image: url('icon.png');
background-size: 32px 32px;
background-repeat: no-repeat;
padding-left: 40px;
}
/* 纹理背景 */
.texture {
background-image: url('texture.png');
background-size: 100px 100px;
background-repeat: repeat;
}
background-attachment 属性用于设置背景图片是否随页面滚动。
/* 随页面滚动(默认) */
.scroll {
background-attachment: scroll;
}
/* 固定背景 */
.fixed {
background-attachment: fixed;
}
/* 随元素内容滚动 */
.local {
background-attachment: local;
}
/* 固定背景效果 */
.parallax {
background-image: url('parallax-bg.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 500px;
}
/* 本地滚动背景 */
.scroll-content {
background-image: url('pattern.png');
background-attachment: local;
background-repeat: repeat;
height: 200px;
overflow: auto;
}
background 简写属性可以同时设置多个背景属性。
/* 完整语法 */
background: background-color background-image background-repeat background-attachment background-position;
/* 示例 */
.box {
background: #fff url('bg.jpg') no-repeat fixed center center;
}
/* 简单背景 */
.card {
background: white;
}
/* 图片背景 */
.hero {
background: url('hero.jpg') no-repeat center center;
background-size: cover;
}
/* 渐变背景 */
.gradient {
background: linear-gradient(to right, #3498db, #9b59b6);
}
/* 复杂背景 */
.complex {
background: #f5f5f5 url('pattern.png') repeat fixed top left;
}
CSS 允许为一个元素设置多个背景。
/* 多个背景图片 */
.multi-bg {
background-image: url('bg1.png'), url('bg2.png');
background-position: top left, bottom right;
background-repeat: no-repeat, no-repeat;
}
/* 颜色和图片混合 */
.color-image {
background: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url('bg.jpg');
background-size: cover;
}
/* 渐变叠加图片 */
.overlay {
background: linear-gradient(rgba(0,0,0,0.6), rgba(0,0,0,0.6)), url('photo.jpg');
background-size: cover;
background-position: center;
}
/* 多层纹理 */
.texture {
background: url('texture1.png'), url('texture2.png');
background-position: top left, bottom right;
background-repeat: repeat, repeat;
}
background-clip 属性用于设置背景的绘制区域。
/* 边框盒(默认) */
.border-box {
background-clip: border-box;
}
/* 内边距盒 */
.padding-box {
background-clip: padding-box;
}
/* 内容盒 */
.content-box {
background-clip: content-box;
}
/* 文本背景 */
.text {
background-clip: text;
-webkit-background-clip: text;
color: transparent;
}
/* 文字渐变效果 */
.gradient-text {
background: linear-gradient(to right, #3498db, #9b59b6);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
font-size: 48px;
font-weight: bold;
}
/* 内边距盒背景 */
.padding-bg {
background: #f5f5f5;
padding: 20px;
border: 10px solid #333;
background-clip: padding-box;
}
background-origin 属性用于设置背景图片的定位区域。
/* 边框盒 */
.border-box {
background-origin: border-box;
}
/* 内边距盒(默认) */
.padding-box {
background-origin: padding-box;
}
/* 内容盒 */
.content-box {
background-origin: content-box;
}
/* 内容盒定位 */
.content {
background: url('icon.png') no-repeat;
background-origin: content-box;
padding: 20px;
}
.card {
background: white;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
overflow: hidden;
}
.card-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
}
.card-body {
background: #f8f9fa;
padding: 20px;
}
.card-footer {
background: white;
padding: 15px 20px;
border-top: 1px solid #dee2e6;
}
.button {
padding: 12px 24px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: all 0.3s ease;
}
.button-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.button-primary:hover {
background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}
.nav {
background: linear-gradient(to right, #1e3c72 0%, #2a5298 100%);
color: white;
padding: 0 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.nav-link {
background: transparent;
color: white;
padding: 15px 20px;
display: inline-block;
text-decoration: none;
transition: background 0.3s ease;
}
.nav-link:hover {
background: rgba(255,255,255,0.1);
}
body {
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
min-height: 100vh;
}
.pattern-bg {
background: #f5f5f5 url('pattern.png') repeat fixed top left;
}
背景属性是 CSS 中重要的属性类别,它们控制着元素的背景外观:
记住以下几点: