使用 CSS 设置元素背景样式

cssweb developmentfront end technology更新于 2024/12/10 13:00:17

要设置元素背景样式,您可以设置背景图像和背景位置。此外,还可以设置背景重复,以便水平或垂直重复图像。

background-position

背景位置用于设置背景图像的起始位置。为此,请使用 background-position 属性。

示例

现在让我们看一个例子 −

<!DOCTYPE html>
<html>
<head>
<style>
body {
   background-image: url("https://www.cainiaomax.com/images/Swift.png");
   background-repeat: no-repeat;
   background-attachment: fixed;
   color: blue;
   background-position: left center;
}
.demo {
   text-decoration: overline underline;
}
</style>
</head>
<body>
<h1>Details</h1>
<p class="demo">Examination Center near ABC College.</p>
<p class="demo2">Exam begins at 9AM.</p>
</body>
</html>

输出

background-repeat

CSS 中的背景重复用于设置背景图像在网页上的重复方式。为此,请使用 background-repeat 属性。以下是属性值 −

background-repeat: repeat|repeat-x|repeat-y|no-repeat|initial|inherit;

示例

现在来看一个例子 −

<!DOCTYPE html>
<html>
<head>
<style>
body {
   background-image: url("https://www.cainiaomax.com/images/Swift.png");
   background-repeat: repeat-x;
   background-color: blue;
   color: white;
}
.demo {
   text-decoration: overline underline;
}
</style>
</head>
<body>
<h1>Details</h1>
<p class="demo">Examination Center near ABC College.</p>
<p class="demo2">Exam begins at 9AM.</p>
</body>
</html>

输出


相关文章


有用资源