CSS背景属性用于添加背景效果 对于元素。
在这些章节中,您将了解以下 CSS 背景属性:
style="color:crimson">背景颜色
style="color:crimson">背景图像
style="color:crimson">背景重复
style="color:crimson">背景附件
style="color:crimson">背景位置
style="color:crimson">背景
(简写属性)
background-color
属性指定元素的背景颜色。
页面的背景颜色设置如下:
body {
background-color: lightblue;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>This page has a light blue background color!</p>
</body>
</html>
对于 CSS,颜色通常由以下方式指定:
有效的颜色名称 - 例如“红色”
十六进制值 - 例如“#ff0000”
RGB 值 - 如“rgb(255,0,0)”
查看 CSS 颜色值以获得完整的 可能的颜色值列表。
您可以设置任何 HTML 元素的背景颜色:
这里,<h1>、<p> 和 <div> 元素将具有不同的背景颜色:
h1 {
background-color: green;
}
div {
background-color: lightblue;
}
p {
background-color:
yellow;
}
自己尝试一下→
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
background-color: green;
}
div {
background-color: lightblue;
}
p {
background-color: yellow;
}
</style>
</head>
<body>
<h1>CSS background-color example!</h1>
<div>
This is a text inside a div element.
<p>This paragraph has its own background color.</p>
We are still in the div element.
</div>
</body>
</html>
opacity
属性指定元素的不透明度/透明度。它可以取 0.0 - 1.0 之间的值。值越低,越透明:
opacity 1
opacity 0.6
opacity 0.3
opacity 0.1
div {
background-color: green;
opacity: 0.3;
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: green;
}
div.first {
opacity: 0.1;
}
div.second {
opacity: 0.3;
}
div.third {
opacity: 0.6;
}
</style>
</head>
<body>
<h1>Transparent Boxes</h1>
<p>When using the opacity property to add transparency to the background of an element, all of its child elements become transparent as well. This can make the text inside a fully transparent element hard to read:</p>
<div class="first">
<h1>opacity 0.1</h1>
</div>
<div class="second">
<h1>opacity 0.3</h1>
</div>
<div class="third">
<h1>opacity 0.6</h1>
</div>
<div>
<h1>opacity 1 (default)</h1>
</div>
</body>
</html>
注意:当使用opacity
属性为元素的背景添加透明度时,其所有子元素 继承相同的透明度。这可能会使完全透明元素内的文本难以阅读。
如果您不想对子元素应用不透明度(如上面的示例所示),请使用 RGBA 颜色值。 以下示例设置背景颜色而不是文本的不透明度:
100% opacity
60% opacity
30% opacity
10% opacity
您从 CSS 颜色章节中了解到,您可以使用 RGB 作为颜色值。除了RGB之外, 您可以使用带有 alpha 通道 (RGBA) 的 RGB 颜色值 - 它指定颜色的不透明度。
RGBA 颜色值通过以下方式指定:rgba(red, green, blue, alpha)。这 alpha 参数是 0.0(完全透明)和 1.0(完全不透明)之间的数字。
提示:您将在我们的 CSS 颜色章节中了解有关 RGBA 颜色的更多信息。
div {
background: rgba(0, 128, 0, 0.3) /* Green background with 30% opacity */
}
自己尝试一下 →
<!DOCTYPE html>
<html>
<head>
<style>
div {
background: rgb(0, 128, 0);
}
div.first {
background: rgba(0, 128, 0, 0.1);
}
div.second {
background: rgba(0, 128, 0, 0.3);
}
div.third {
background: rgba(0, 128, 0, 0.6);
}
</style>
</head>
<body>
<h1>Transparent Boxes 2</h1>
<p>Result with opacity:</p>
<div style="opacity:0.1;">
<h1>10% opacity</h1>
</div>
<div style="opacity:0.3;">
<h1>30% opacity</h1>
</div>
<div style="opacity:0.6;">
<h1>60% opacity</h1>
</div>
<div>
<h1>opacity 1</h1>
</div>
<p>Result with rgba():</p>
<div class="first">
<h1>10% opacity</h1>
</div>
<div class="second">
<h1>30% opacity</h1>
</div>
<div class="third">
<h1>60% opacity</h1>
</div>
<div>
<h1>default</h1>
</div>
<p>Notice how the text gets transparent as well as the background color when using the opacity property.</p>
</body>
</html>
设置元素的背景颜色