1. 背景图片重复的默认行为

/* 默认重复背景图片 */
.background-image {
  background-image: url('image.jpg');
  background-repeat: repeat;
}

2. 设置背景图片不重复

/* 设置背景图片不重复 */
.background-image {
  background-image: url('image.jpg');
  background-repeat: no-repeat;
}

3. 控制背景图片的位置

/* 设置背景图片的起始位置 */
.background-image {
  background-image: url('image.jpg');
  background-repeat: no-repeat;
  background-position: center center; /* 将图片居中显示 */
}

此外,background-position还可以使用百分比或关键字(如top, right, bottom, left)来设置。

4. 示例代码

以下是一个完整的HTML和CSS示例,展示了如何使用上述技巧:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>背景图片不重复示例</title>
<style>
  .background-image {
    width: 100%;
    height: 300px;
    background-image: url('image.jpg');
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover; /* 确保图片覆盖整个元素 */
  }
</style>
</head>
<body>
<div class="background-image"></div>
</body>
</html>

5. 总结