背景相关的CSS属性
本文解释了背景相关的CSS属性。
您可以学习如何描述背景设置、位置和重复等属性。
YouTube Video
创建用于预览的 HTML
css-background.html
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>CSS Properties Example</title>
7 <link rel="stylesheet" href="css-base.css">
8 <link rel="stylesheet" href="css-background.css">
9</head>
10<body>
11 <!-- Header -->
12 <header>
13 <h1>Background-Related CSS Properties Example</h1>
14 </header>
15
16 <!-- Main content -->
17 <main>
18 <header>
19 <h2>Background And Decoration</h2>
20 </header>
21 <article>
22 <h3>background-color</h3>
23 <section>
24 <header><h4>background-color: red</h4></header>
25 <section class="sample-view">
26 <div class="red-example">This is a red background.</div>
27 </section>
28 <header><h4>background-color: #FF5733</h4></header>
29 <section class="sample-view">
30 <div class="hex-example">This is a background with hex color (#FF5733).</div>
31 </section>
32 <header><h4>background-color: rgb(255, 87, 51)</h4></header>
33 <section class="sample-view">
34 <div class="rgb-example">This is a background with RGB color (rgb(255, 87, 51)).</div>
35 </section>
36 <header><h4>background-color: rgba(255, 87, 51, 0.7)</h4></header>
37 <section class="sample-view">
38 <div class="rgba-example">This is a background with RGBA color (rgba(255, 87, 51, 0.7)).</div>
39 </section>
40 <header><h4>background-color: hsl(14, 100%, 60%)</h4></header>
41 <section class="sample-view">
42 <div class="hsl-example">This is a background with HSL color (hsl(14, 100%, 60%)).</div>
43 </section>
44 <header><h4>background-color: hsla(14, 100%, 60%, 0.7)</h4></header>
45 <section class="sample-view">
46 <div class="hsla-example">This is a background with HSLA color (hsla(14, 100%, 60%, 0.7)).</div>
47 </section>
48 <header><h4>background-color: transparent</h4></header>
49 <section class="sample-view">
50 <div class="transparent-example">This is a background with transparency (transparent).</div>
51 </section>
52 </section>
53 </article>
54 <article>
55 <h3>background-image</h3>
56 <section>
57 <header><h4>background-image: url('image.jpg')</h4></header>
58 <section class="sample-view">
59 <div class="background-image-example">This is a background image.</div>
60 </section>
61 <header><h4>background-image: linear-gradient(to right, red, blue)</h4></header>
62 <section class="sample-view">
63 <div class="background-image-gradient">This is a background gradient.</div>
64 </section>
65 </section>
66 </article>
67 <article>
68 <h3>background-position</h3>
69 <section>
70 <header><h4>background-position: top left</h4></header>
71 <section class="sample-view">
72 <div class="top-left-example">Top Left</div>
73 </section>
74 <header><h4>background-position: center</h4></header>
75 <section class="sample-view">
76 <div class="center-example">Center</div>
77 </section>
78 <header><h4>background-position: bottom right</h4></header>
79 <section class="sample-view">
80 <div class="bottom-right-example">Bottom Right</div>
81 </section>
82 </section>
83 </article>
84 <article>
85 <h3>background-size</h3>
86 <section>
87 <header><h4>background-size: cover</h4></header>
88 <section class="sample-view">
89 <div class="cover-example">Cover</div>
90 </section>
91 <header><h4>background-size: contain</h4></header>
92 <section class="sample-view">
93 <div class="contain-example">Contain</div>
94 </section>
95 <header><h4>background-size: 100px 100px</h4></header>
96 <section class="sample-view">
97 <div class="fixed-size-example">100px by 100px</div>
98 </section>
99 </section>
100 </article>
101 <article>
102 <h3>background-repeat</h3>
103 <section>
104 <header><h4>background-repeat: repeat</h4></header>
105 <section class="sample-view">
106 <div class="repeat-example">Repeat</div>
107 </section>
108 <header><h4>background-repeat: repeat-x</h4></header>
109 <section class="sample-view">
110 <div class="repeat-x-example">Repeat X</div>
111 </section>
112 <header><h4>background-repeat: no-repeat</h4></header>
113 <section class="sample-view">
114 <div class="no-repeat-example">No Repeat</div>
115 </section>
116 <header><h4>background-repeat: space</h4></header>
117 <section class="sample-view">
118 <div class="space-example">Space</div>
119 </section>
120 <header><h4>background-repeat: round</h4></header>
121 <section class="sample-view">
122 <div class="round-example">Round</div>
123 </section>
124 </section>
125 </article>
126 </main>
127</body>
128</html>背景和装饰
background-color 属性
1/* Background color specification */
2.red-example {
3 background-color: red;
4}
5
6.hex-example {
7 background-color: #FF5733;
8}
9
10.rgb-example {
11 background-color: rgb(255, 87, 51);
12}
13
14.rgba-example {
15 background-color: rgba(255, 87, 51, 0.7);
16}
17
18.hsl-example {
19 background-color: hsl(14, 100%, 60%);
20}
21
22.hsla-example {
23 background-color: hsla(14, 100%, 60%, 0.7);
24}
25
26.transparent-example {
27 background-color: transparent;
28 color: black;
29}background-color 属性用于在CSS中设置元素的背景颜色。您可以指定显示在文本及其他元素后面的颜色,并且颜色可以用多种格式定义。指定颜色的方法与 color 属性类似,但您也可以使用 transparent 指定完全透明的背景。
解释:
red-example类通过关键字将背景颜色指定为红色。hex-example类通过十六进制代码指定背景颜色。rgb-example类通过 RGB 格式指定背景颜色。rgba-example类通过 RGBA 格式指定背景颜色,并添加透明度。hsl-example类通过 HSL 格式指定背景颜色。hsla-example类通过 HSLA 格式指定背景颜色,并添加透明度。transparent-example类将背景设置为透明。
background-image 属性
1/* Background image styles */
2.background-image-example {
3 /* Specify the image URL */
4 background-image: url('image.jpg');
5 /* Scale the image to cover the entire element */
6 background-size: cover;
7 /* Center the image */
8 background-position: center;
9 /* Disable image repetition */
10 background-repeat: no-repeat;
11 color: white;
12 display: flex;
13 align-items: center;
14 justify-content: center;
15}
16
17/* Gradient background styles */
18.background-image-gradient {
19 /* Gradient from left to right */
20 background-image: linear-gradient(to right, red, blue);
21 color: white;
22 display: flex;
23 align-items: center;
24 justify-content: center;
25}background-image 属性用于将图像设置为元素的背景。指定的图像将显示为元素的背景,其大小、位置和重复方式可以通过其他相关属性进行调整。我们还将在后面解释类似 background-size、background-position 和 background-repeat 相关的属性。
解释:
-
background-image-example类使用background-image属性将image.jpg设置为背景。background-size: cover使图像覆盖整个元素,而background-position: center将图像居中。使用background-repeat: no-repeat禁用重复。 -
类
background-image-gradient使用linear-gradient()将背景设置为从红到蓝的渐变。渐变从左到右显示。
可以指定的值的类型
background-image 属性可以接受以下值。
url():指定背景图像的 URL。在url()中包含图像文件。 (ex.url('image.jpg'))none:指定不设置背景图像。这是默认值。- 渐变:也可以通过 CSS 的渐变功能将渐变设置为背景图像。 (ex.
linear-gradient(),radial-gradient())
background-image 属性的关键点
-
图像大小和定位:可以通过其他属性精细控制背景图像的大小和定位,从而进行设计调整。例如,指定
background-size: cover会将图像拉伸以覆盖整个区域,从而消除任何剪裁。 -
使用渐变:您可以用渐变作为背景,而不是图像,从而为设计增添动态元素。
background-position 属性
1/* Positioned at the top-left */
2.top-left-example {
3 background-image: url('image.jpg');
4 background-position: top left;
5 background-size: cover;
6 background-repeat: no-repeat;
7 background-color: lightblue;
8 height: 100px;
9}
10
11/* Centered */
12.center-example {
13 background-image: url('image.jpg');
14 background-position: center;
15 background-size: cover;
16 background-repeat: no-repeat;
17 background-color: lightcoral;
18 height: 100px;
19}
20
21/* Positioned at the bottom-right */
22.bottom-right-example {
23 background-image: url('image.jpg');
24 background-position: bottom right;
25 background-size: cover;
26 background-repeat: no-repeat;
27 background-color: lightgreen;
28 height: 100px;
29}background-position 属性用于指定背景图像在元素内的位置。通过控制背景图像在元素中的显示位置,可以创建与设计相匹配的布局。默认值是 0% 0%,将图像放置在左上角。
解释:
top-left-example类将图片位置指定为top left,将图片放置在左上角。center-example类使用center属性将图片放置在中心。bottom-right-example类将图片位置指定为bottom right,将图片放置在右下角。
可以指定的值的类型
background-position 属性可以接受以下类型的值。
-
关键字:可以指定
left、right、top、bottom和center。- 指定
center将把背景图像放置在元素的中心。 - 指定
right将把背景图像放置在右侧。 - 指定
top left将把背景图像放置在左上角。 - 指定
bottom right将会把背景图片放置在右下角的位置。
- 指定
-
长度或百分比:您可以指定如
10px 20px、50% 50%这样的值。- 指定
10px 20px将会把背景图片放置在距离左边 10px 和顶部 20px 的位置。 - 指定
50% 50%将会使背景图片在水平方向和垂直方向居中。
- 指定
-
calc()函数:允许通过 CSS 计算精确定位。
background-size 属性
1/* Fit the image to cover the entire area */
2.cover-example {
3 background-image: url('image.jpg');
4 /* The image covers the entire element */
5 background-size: cover;
6 background-color: lightblue;
7}
8
9/* Fit the image within the element */
10.contain-example {
11 background-image: url('image.jpg');
12 /* The image fits within the element */
13 background-size: contain;
14 background-color: lightgreen;
15}
16
17/* Display at a fixed size */
18.fixed-size-example {
19 background-image: url('image.jpg');
20 /* Fixed width of 100px and height of 100px */
21 background-size: 100px 100px;
22 background-color: lightcoral;
23}background-size 属性用于指定元素的背景图片的大小。通过此属性,您可以调整背景图片的显示方式,无论是填满整个元素还是保持原始大小,以适应设计需要。默认值是 auto,即保持背景图片的原始大小。
解释:
cover-example类指定背景为cover。图片将会被放大以覆盖整个元素,但部分可能会被裁剪。contain-example类指定背景为contain。图片将会被调整以适应元素范围,但可能会出现一些空白区域。fixed-size-example类指定背景图片的固定大小,宽度和高度均为 100px。
可以指定的值的类型
background-size 属性可以被赋予以下类型的值。
-
关键词
auto:保持图片的默认尺寸(宽度和高度会自动决定)。cover:调整背景图片尺寸来完全覆盖元素。它会填满整个元素,但图片的部分可能会被剪裁。contain:调整图片以适应元素范围,但不会覆盖整个元素。图片的宽高比将被保持。
-
数值(
px、%、em等)- 宽度和高度:明确指定宽度和高度。如果只指定了一个值,则该值用于宽度,高度会自动调整。
- 百分比:将背景图片的尺寸指定为元素大小的百分比。例如,
50% 50%将图像设置为元素宽度和高度的一半。
background-repeat 属性
1/* Repeat vertically and horizontally */
2.repeat-example {
3 background-image: url('pattern.png');
4 background-repeat: repeat;
5 background-size: auto;
6}
7
8/* Repeat only horizontally */
9.repeat-x-example {
10 background-image: url('pattern.png');
11 background-repeat: repeat-x;
12 background-size: auto;
13}
14
15/* No repetition */
16.no-repeat-example {
17 background-image: url('pattern.png');
18 background-repeat: no-repeat;
19 background-size: auto;
20}
21
22/* Space out evenly */
23.space-example {
24 background-image: url('pattern.png');
25 background-repeat: space;
26 background-size: auto;
27 width: 90px;
28 height: 60px;
29}
30
31/* Resize and repeat */
32.round-example {
33 background-image: url('pattern.png');
34 background-repeat: round;
35 background-size: auto;
36}background-repeat 属性用于控制元素的背景图像如何重复。默认情况下,背景图像会在元素内水平和垂直重复,但您可以使用此属性自定义重复行为。
解释:
repeat-example类让图片在垂直和水平方向上同时重复显示。repeat-x-example类让图片仅在水平方向上重复显示。no-repeat-example类仅显示一次图片,不进行重复。space-example类让背景图片均匀排列,保持图片间隔相等。round-example类自动调整背景图片的大小以重复并填满整个元素。
可以指定的值
background-repeat 属性可以被赋予以下类型的值。
repeat:背景图像沿 X 轴(水平方向)和 Y 轴(垂直方向)重复。这是默认值。repeat-x:背景图像仅沿 X 轴(水平方向)重复。repeat-y:背景图像仅沿 Y 轴(垂直方向)重复。no-repeat:背景图像不重复,仅显示一次。space:背景图像以固定的间隔重复,间距均等。round:背景图像会重复,但大小会调整以填满整个元素。图像可能会调整大小。
您可以在我们的YouTube频道上使用Visual Studio Code跟随上述文章进行学习。 请也查看我们的YouTube频道。