在 CSS 中设置背景颜色

cssweb developmentfront end technology更新于 2025/1/5 3:24:17

我们可以使用 background-color 属性指定元素的背景颜色。我们可以使用标准名称 rgb()、rgba()、hsl() 或 hsla() 来指定值。

语法

CSS background-color 属性的语法如下 −

Selector {
   background-color: /*value*/
}

示例

以下示例说明了 CSS background-color 属性 −

<!DOCTYPE html>
<html>
<head>
<style>
p:first-of-type {
   font-family: cursive;
   background-color: pink;
}
p:last-child {
   background-color: azure;
}
div {
   margin: auto;
   padding: 5px;
   font-family: Helvetica;
   width: 430px;
   background-color: burlywood;
}
</style>
</head>
<body>
<p>
Java is a high-level programming language originally developed by Sun Microsystems.<p>
<div>
Java released in 1995</div>
<p>
Java runs on a variety of platforms such as Windows, Mac OS, and the various versions of UNIX.</p>
</body>
</html>

输出

这将给出以下输出

示例

<!DOCTYPE html>
<html>
<head>
<style>
div {
   background-color: springgreen;
}
h1 {
   background-color: lightcoral;
   text-align: center;
}
p {
   background-color: lightseagreen;
   font-size: 150%;
}
</style>
</head>
<body>
<div>
<h1>Demo</h1>
<p>This is demo text1. This is demo text2. This is demo text3. </p>
</div>
</body>
</html>

输出

这将给出以下输出 −


相关文章


有用资源